-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swift Optional Values from JSON #116
Comments
I'm seeing this too. |
I'm also getting this issue, running XCode 6.1.1. If you run this code on an actual device you'll get a slightly different exception. I think there's a safety check/assert that's failing, because the code is trying to access an object that's already been released. But there shouldn't be any object accessed, because this key is supposed to be missing, right? |
I'm not exactly sure what's happening with that optional value in self.object[key] yet but this seems to fix the issue. var tmpObject: AnyObject? = self.object[key] ?? nil
if let object_: AnyObject = tmpObject {
returnJSON = JSON(object_)
} else {
returnJSON._error = NSError(domain: ErrorDomain, code: ErrorNotExist, userInfo: [NSLocalizedDescriptionKey: "Dictionary[\"\(key)\"] does not exist"])
} |
+1 |
1 similar comment
+1 |
I found another possible patch for if self.type == .Dictionary {
if let object_: AnyObject = (self.object as [String : AnyObject])[key] { // <-- casting here
returnJSON = JSON(object_)
} else {
returnJSON._error = NSError(domain: ErrorDomain, code: ErrorNotExist, userInfo: [NSLocalizedDescriptionKey: "Dictionary[\"\(key)\"] does not exist"])
}
} |
Hi, |
Anyone else having a problem with getting the swift optional examples working? If the "key" doesn't exist in the JSON file, it throws an error instead of returning nil.
If I use an "if let" construct like in the examples it does not work nor does just setting a variable like:
let test: Float? = item["color","red"].float
and if "red" does not exist in the JSON file, it throws an "EXC_BAD_INSTRUCTION" error at this line in the dictionary subscript getter in SwiftyJSON:
if let object_: AnyObject = self.object[key] {
Full getter below:
The JSON file works with (pulled from a larger JSON file that validates in a validator):
but if you remove the "red" entry you get that error with the Optional.
The text was updated successfully, but these errors were encountered: