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)
Related
I'm stuck on a weird problem.
I'm parsing a JSON response via Alamofire, and want to fetch a variable that just keeps throwing errors regardless whether I cast it to a String or Number, whilisting changing the error message every time I do :/
If I cast it to a String like this:
let kolicina = jsonCategory["kolicina"] as! String
if Int(kolicina) > 0 {
I get the error:
"Could not cast value of type '__NSCFNumber' (0x10f051368) to 'NSString' (0x10e627b48)."
When I try to cast it to a NSNumber:
let kolicina = jsonCategory["kolicina"] as! NSNumber
if kolicina.integerValue > 0 {
I get:
"Could not cast value of type 'NSTaggedPointerString' (0x10effcae8) to 'NSNumber' (0x10e5d32a0)."
This is what I get for that specific variable when I print the Dictionary:
kolicina = 0;
Can someone point me in the right direction?
It seems you have a combination of strings and integers in there. I would recommend you to fix the JSON and keep the data either as a string or a number, not both.
If you want to parse such a format, you will have to try to parse it as a NSNumber or as a NSString, e.g.:
let kolicina = jsonCategory["kolicina"]
if let kolicinaString = kolicina as? String {
...
} else if let kolicinaInt = kolicina as? Int {
...
}
with the > 0 checks you could do something like this:
if let kolicinaString = kolicina as? String,
kolicinaInt = Int(kolicinaString) where kolicinaInt > 0 {
print("String: \(kolicinaInt)")
} else if let kolicinaInt = kolicina as? Int where kolicinaInt > 0 {
print("Int: \(kolicinaInt)")
}
or using a ternary operator:
let kolicinaInt = (kolicina is String) ? Int(kolicina as! String) : kolicina as? Int
if kolicinaInt > 0 {
print("Kolicina: \(kolicinaInt)")
}
Try this .. don't make force unwrap
if let kolicina = jsonCategory["kolicina"] as? Int{
// Success: your kolicina is Int
print(kolicina)
}
else{
// Not an Int
}
I have a variable content from a NSArray : let content = application["content"]!
When I print content, I have a String :
print(content) -> My content
But when I want to cast my variable to String : let content = application["content"]! as! String
I can't print my variable because it's null :
print(content) -> Could not cast value of type 'NSNull' (0x1a0507768) to 'NSString' (0x1a0511798).
Why ?
UPDATE :
My array when value is not casted :
{
"application_title" = "Marina Kaye";
"application_type" = discussions;
"application_type_name" = Discussions;
content = (
{
content = "Le nouvel album de Marina Kaye";
link = "?message_id=118";
},
{
content = "Son album est num\U00e9ro 1 des";
link = "?message_id=131";
},
{
content = "Le nouvel album s'appel";
link = "?message_id=126";
}
);
"content_title" = "Messages utiles";
"content_type" = "useful_messages";
}
My array when value is casted :
{
"application_title" = "Marina Kaye";
"application_type" = discussions;
"application_type_name" = Discussions;
content = "<null>";
"content_title" = "<null>";
"content_type" = "usefull_messages";
}
I can't cast content to NSArray and content_title to String.
MY CODE :
let applicationsArray = result["applications"]! as! NSArray
for application in applicationsArray {
let applicationTitle = application["application_title"]! as! String
let applicationType = application["application_type"]! as! String
let applicationTypeName = application["application_type_name"]! as! String
let content = application["content"]! as! NSArray
let contentTitle = application["content_title"]! as! String
let contentType = application["content_type"]! as! String
self.listApplications.append(Application(applicationTitle: applicationTitle, applicationType: applicationType, applicationTypeName: applicationTypeName, content: content, contentTitle: contentTitle, contentType: contentType))
}
As you are coding in Swift, you do not need the legacy NSArray and NSDictionary types. Instead, these are now Array and Dictionary, but you do not even have to care about that.
To declare an array, you usually specify the type in square brackets, such as [String]. For a dictionary, you need this for both key and value, separated by a colon, e.g. [String: AnyObject].
From your log output, you have a Dictionary of type [String: AnyObject] with 6 keys; all of them point to String objects, except the "content" one.
The "content" key apparently points to an array of dictionaries. This is written like this: [[String: AnyObject]]. Thus it is not surprising that casting this to String is not successful.
Here is how you can parse the application dictionary's "content":
if let content = application["content"] as? [[String: AnyObject]] {
for item in content {
let text = content["content"] as? String
let link = content["link"] as? String
// do something with this data
}
}
I would recommend defining a class or struct to capture the application object. Your code will be much clearer and easier to maintain.
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))"
I really thought this was Xcode playing up as I can't for the life of me see what is causing this error in a fairly simple piece of Swift code but after restarting cleaning the project, restarting Xcode, and restarting my MacBook I'm still getting the same error.... But why!!??!!
Here is the code
// Loop through records saving them
for recordItem in records {
if let record: NSDictionary = recordItem as? NSDictionary {
let time: String = record["time"] as! String
let record: String = record["record"] as! String
let recordId: String = record["record_id"] as! String
let saveTime: String = record["save_time"] as! String
let setId: String = record["set_id"] as! String
// Does more stuff
}
}
records is an NSArray and it is made from data downloaded from a remote server.
All is fine until the line beginning let recordId. This and the two lines below it are giving an error - Cannot subscript a value of type 'String' with an index of type 'String'
Why? And why is there no problem with the 2 lines above? And how do I fix it?
Any help is greatly appreciated as I'm at a bit of a dead end.
You are using the variable record to unwrap the optional recordItem but then you are re-assigning record as record["record"] - so from this point on it becomes a String, not a Dictionary - resulting in the error message Cannot subscript a value of type 'String' with an index of type 'String'
You just need to change one of the record variables
for recordItem in records {
if let unwrappedRecord: NSDictionary = recordItem as? NSDictionary {
let time: String = unwrappedRecord["time"] as! String
let record: String = unwrappedRecord["record"] as! String
let recordId: String = unwrappedRecord["record_id"] as! String
let saveTime: String = unwrappedRecord["save_time"] as! String
let setId: String = unwrappedRecord["set_id"] as! String
// Does more stuff
}
}
The problem is here, you are declaring record as a String, previously it was NSDictionary and due to scope the record in record["record_id"] is String not NSDictionary. Quick fix is to change the name as I did
let time: String = record["time"] as! String
let record1: String = record["record"] as! String
let recordId: String = record["record_id"] as! String
let saveTime: String = record["save_time"] as! String
let setId: String = record["set_id"] as! String
Why do I get an error when I used valueForKey... I am using same trick like in objectiveC ...
In ObjectiveC, the code is
self.strSubscribe =[responseObject[#"subscribe"] valueForKey:#"subscribe_ids"];
In Swift , the code is
self.strSubscribe = responseObject["subscribe"].valueForKey["subscribe_ids"] as! String
I declare the variables like
var arraySubCategory : NSMutableArray! = NSMutableArray()
var strSubscribe:String!
And I tried to access the value from below response
{
subscribe =
{
"subscribe_ids" = "1,14";
}
}
Edit
It works using Amit and Eric's solution but now for following data
{
data = (
{
"subscribe_ids" = "1,14";
}
);
}
let dictionary = responseObject["data"][0] as! Dictionary<String,AnyObject>
self.strSubscribe = dictionary["subscribe_ids"] as! String
OR//
if let dic = responseObject["data"][0] as? [String:String], let ids = dic["subscribe_ids"] {
self.strSubscribe = ids
}
but it gives me error:
could not find member 'subscript'
Swift doesn't know the type of responseObject["subscribe"], you have to help the compiler a bit; for example:
if let dic = responseObject["subscribe"] as? [String:String], let ids = dic["subscribe_ids"] {
self.strSubscribe = ids // "1,14"
}
UPDATE:
It's still the same problem: the compiler doesn't know the type of responseObject["data"], so when you try to access the subscript there's an error (because you know it's a dictionary inside the array, but the compiler doesn't).
One solution is to give the type to the compiler by declaring an array of dictionaries in the if let condition:
if let arr = responseObject["data"] as? [[String:String]], let ids = arr[0]["subscribe_ids"] {
self.strSubscribe = ids
}
Notice that it's [[String:String]] (array of dictionaries), not [String:String] (dictionary).
Write like this.
let dictionary = responseObject["subscribe"] as! Dictionary<String, AnyObject>
self.strSubscribe = dictionary["subscribe_ids"] as! String
Since responseObject["subscribe"] will give a AnyObject? output and AnyObject does not have any member called valueForKey.