after updating xcode I simply can't remove the optional() from my string?
retrievedUsername = KeychainWrapper.stringForKey("username")!
this prints out
optional("HK")
but I need it to be
HK
I've tried
if let username = KeychainWrapper.stringForKey("username"){
retrievedUsername = username
}
but no luck!
any ideas?
Your value is probably an optional containing an optional, so you'll have to unwrap it twice:
if let temp = KeychainWrapper.stringForKey("username"), let username = temp {
retrievedUsername = username
}
If this doesn't work, this is because it's not a double optional, and it means that your original string already contains the text "Optional(HK)" due to a prior error.
I found out the problem, it would save to the keychain with "optional" so when it retrieves the string it is "optional("HK")" that's why unwrapping it didn't work
Related
When I create a variable without String() to initialize the variable an error shows up alerting, "variable not initialize", same error shows up when I directly assign
var username: String = usernameTextfieldSigup.text!
And when I initialize the variable using the code below,
var username: String = String()
username = usernameTextfieldSignup.text!
warning comes up, variable 'username' was written but never used.
I know I could just ignore these warnings but I'd like a cleaner code.
I'm using Xcode7 beta5 release.
You can declare the username without a value first:
var username: String
and then assign a value to it in your initializer:
// replace with your (valid) initializer
func init() {
username = usernameTextfieldSignup.text!
}
However, I'm sure that usernameTextfieldSignup.text won't have a value until the user ha actually provided a value. So in that case I would recommend to make username an optional so you can set it's value when you need to:
var username: String?
and then when setting it:
username = usernameTextfieldSignup.text
and when accessing it:
if let uname = username {
// do something with the uname
}
Also, the variable 'username' was written but never used warning you're seeing is probably because you write to / initialise username but there's no code actually reading it, so it's kind of a useless variable according to the compiler then.
To init an empty string you have to either make it explicit or optional like so:
let string: String!
let string: String?
You can do this with any datatype.
I wanted to make an application that checks if a link was blocked on steam.
I used the linkfilter page. (https://steamcommunity.com/linkfilter/?url=)
I tried doing it like this:
WebBrowser1.Url = https://steamcommunity.com/linkfilter/?url=(TextBox1.Text)
But I got two errors. Is there a way to do this?
Did you try setting your string values as actual strings?:
WebBrowser1.Url = new Uri("https://steamcommunity.com/linkfilter/?url=" + TextBox1.Text);
or:
WebBrowser1.Url = new Uri(string.Format("https://steamcommunity.com/linkfilter/?url={0}", TextBox1.Text));
This line of code in Swift causes me problems when the address book has a contact with no last name.
I've tried to resolve it a number of ways to no avail. Is there some sort of try catch statement or error handling I can use? Or check if AnyObject is null (the return type of
ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue()).
I've tried using optional types but it doesn't seem to work since the app stops running the moment you select a contact with no last name - and the line of code below gets highlighted with the error Thread
1: EXC_BAD_ACCESS
let lName = ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue() as String
ABRecordCopyValue can actually return nil so you should unwrap it. Also, casting to String didn't work for me so I'm using NSString.
if let firstName = ABRecordCopyValue(abContact, kABPersonFirstNameProperty)?.takeRetainedValue() as? NSString {
println("FIRST NAME: \(firstName)")
}
else {
println("No Name")
}
Another thing you could try is instead of getting the first and lastName individually, you could also try to get the composed name.
if let fullName = ABRecordCopyCompositeName(abContact)?.takeRetainedValue() as String? {
println("Full Name: \(fullName)")
}
P.S: I've also tried all the answers from this question but despite of making perfect sense and working when debugging the app, they crashed when I deployed the archive directly into the phone.
Im sure this is very obvious but I cant seem to get it work.
I have a Pathname instance and Im attempting to remove the first directory from it and then return the rest of the string, but because slice returns the part removed it seems there is no way of getting the smaller string back.
filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filedir = filepath.to_s.slice("this_folder_needs_to_go/")
newfilepath = filedir
I would hope newfilepath would be another_folder/file.html but instead it just returns this_folder_needs_to_go/
So how on earth to I get the string that has had the part removed?
Using String#split:
"this_folder_needs_to_go/another_folder/file.html".split('/', 2)[1]
# => "another_folder/file.html"
You can also use .slice! instead of .slice if you're trying to modify your string
filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filepath.to_s.slice!("this_folder_needs_to_go/")
puts filepath ==> "another_folder/file.html"
Though that will affect your filepath variable irreversibly.
#partition will also work in a similar manner to #split
filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
file_dir = filepath.partition("this_folder_needs_to_go/")[-1]
file_dir
#=> "another_folder/file.html"
#partition returns [head,sep,tail] so this will look like ["","this_folder_needs_to_go","/another_folder/file.html"] the [-1] says get the last element.
End user can upload files in a Visualforce page. In the backend I need to create a list of allowed file extensions, and restrict the user to that list. How do I create the extensions list and validate it? Any help is very much appreciated.
You don't need apex for that?
<apex:inputFile> has accept parameter which you can use. Bear in mind this will check contentType, not extension (which is bit more proper way to do it).
If you still want the validation in apex - probably something like this?
String fileName = 'foobar.xls';
Set<String> acceptedExtensions = new Set<String> {'.doc','.txt', '.jpg'};
Boolean found = false;
for(String s : acceptedExtensions){
if(found = fileName.endsWith(s)){ // yes, there's only one "=", I do want assignment here
break;
}
}
if(!found){ // after the whole loop it's still false?
ApexPages.addMessage(...);
}