I'm trying to pass a text that is correct api put in swift he arrives with the message of "optional". this is my code:
let accountValues:NSArray = account!.accountNumber.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-"))
self.accountTextField.text = accountValues.objectAtIndex(0) as? String
self.accountDigitTextField.text = accountValues.objectAtIndex(1) as? String
When retrieving the value, use ! after the value. The downcast should force unwrap any optionals. Anything set as? can be force unwrapped by using !.
For example,
let intString = 7 as? String
print(intString!)
This should print "7", rather than Optional("7")
please replace these lines :
self.accountTextField.text = accountValues.objectAtIndex(0) as? String
self.accountDigitTextField.text = accountValues.objectAtIndex(1) as? String
by :
self.accountTextField.text = (accountValues.objectAtIndex(0) as! String)
self.accountDigitTextField.text = (accountValues.objectAtIndex(1) as! String)
You're using Optional Value "As? String". So, it will return an Optional('value').
Try this:
let accountValues:NSArray = account!.accountNumber.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-"))
self.accountTextField.text = "\(accountValues.objectAtIndex(0))"
self.accountDigitTextField.text = "\(accountValues.objectAtIndex(1))"
Related
I have recently converted my code to Swift 3.0 from Swift 2
I am getting value like this
let productRequestID = Int(self.array[(indexPath as NSIndexPath).item]["ProductRequest"]!!["product_request_id"] as! NSString as String)!
let requestTitle = ((self.array[(indexPath as NSIndexPath).item]["ProductRequest"]!!["request_title"] as! NSString) as String) as String
Now I am getting an error Type 'Any' has no subscript members
self.array[(indexPath as NSIndexPath).item]["ProductRequest"] This line returns the object in Any so compiler unable to find the key product_request_id as Any type.
So you have to type cast into Dictionary<Any> then you can get your value like this:
let dict = self.array[(indexPath as NSIndexPath).item]["ProductRequest"] as? Dictionary<Any>
let productRequestID = Int(dict!["ProductRequest"]as? String ?? "")
let requestTitle = dict!["request_title"]as? String ?? ""
i swift 2 i was getting the systemLocale using the bellow code:
let systemLocaleCountryCode = NSLocale.systemLocale().objectForKey(NSLocaleCountryCode) as? String
but now i in swift 3 , i am receiving the bellow error :
cannot call value of non function type locale
then once i changed it to :
let systemLocaleCountryCode = NSLocale.systemLocale.objectForKey(NSLocaleCountryCode) as? String
I've received another error:
value of type Locale has no member objectForKey
what's the problem ? How to fix it ?
use like
if let systemLocaleCountryCode = (Locale.system as NSLocale).object(forKey: .countryCode) as? String {
print(systemLocaleCountryCode)
}
In Swift3
change to
let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String
You can try this:
if let systemLocaleCountryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String
{
print(systemLocaleCountryCode)
}
I'm trying to get the data out of a notification in swift 3, using this tutorial: Developing Push Notifications for iOS 10 Unfortunately I get the following error:
private func getAlert(notification: [NSObject:AnyObject]) -> (String, String) {
let aps = notification["aps"] as? [String:AnyObject]
let alert = aps["alert"] as? [String:AnyObject]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
return (title ?? "-", body ?? "-")
}
The issue is that notification is declared as a dictionary with keys of type NSObject. But you attempt to access that dictionary with a key of type String. String is not an NSObject. One solution is to cast your String to NSString.
Fixing that presents another error which is fixed on the next line. So your code ends up like this:
private func getAlert(notification: [NSObject:AnyObject]) -> (String, String) {
let aps = notification["aps" as NSString] as? [String:AnyObject]
let alert = aps?["alert"] as? [String:AnyObject]
let title = alert?["title"] as? String
let body = alert?["body"] as? String
return (title ?? "-", body ?? "-")
}
Having said all of that, that tutorial has a lot of mistakes and uses the wrong parameter types in many places. This getAlert method should not be using NSObject. It should be String.
I'm having a lot of frustration with Swift when it comes to working with Dictionaries and NSDictionaries.
I am pulling data from a server. One of the values is a Bool.
I started with a Swift Dictionary and moved to NSDictionary out of frustration. However, I still cannot get the values from the dictionary.
All of the following fail with contradictory errors:
let unread:Bool = data!["unread"] as! Bool
let unread:Bool = data?["unread"] as! Bool
let unread:Bool = data?.objectForKey("unread") as! Bool
let unread:NSNumber = data?["unread"] as! NSNumber
error: Could not cast value of type 'NSTaggedPointerString' (0x110c1eae8) to 'NSNumber' (0x10e0ab2a0).
Okay, looks like that data is coming in as a String... let's try:
let unreadStr:String = data!["unread"] as! String
let unreadStr:NSString = data!["unread"] as! NSString
error: Could not cast value of type '__NSCFBoolean' (0x1097413b8) to 'NSString' (0x106bcdb48).
So I'm confused. When I try to convert it to a Bool it says I cannot convert a String to a Number. When I try to convert it to a String, it says I cannot convert a number to a string.
Here is what the data looks like:
Optional({
senderId = 10154040;
sent = 1460844986973;
text = "Test Message 1";
unread = false;
})
You should try something along these lines:
let data: [String : Any] = ["first" : "test", "second" : 2, "third" : true]
let first = data["first"] as! String
let second = data["second"] as! Int
let third = data["third"] as! Bool
print(first)
print(second)
print(third)
let debitCardNum = responseObject!["card_number"] as! String
This is my code and I am getting this error
Could not cast value of type 'NSNull' (0x384db1b4) to 'NSString'
(0x384e073c).
use let do like
if let Value = responseObject!["card_number"] as? String {
debitCardNum = Value
}
else
{
//error
}
This is a good candidate for optionals.
let debitCardNum = responseObject["card_number"] as? String
Later on when you want to do something use an if let to ensure the variable has a value
if let debitCardNum = debitCardNum {
// do something
}