Skip to content
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

Closed
rebump opened this issue Dec 17, 2014 · 8 comments
Closed

Swift Optional Values from JSON #116

rebump opened this issue Dec 17, 2014 · 8 comments

Comments

@rebump
Copy link

rebump commented Dec 17, 2014

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:

  private subscript(#key: String) -> JSON {
    get {
      var returnJSON = JSON.nullJSON
      if self.type == .Dictionary {
        if let object_: AnyObject = self.object[key] {
          returnJSON = JSON(object_)
        } else {
          returnJSON._error = NSError(domain: ErrorDomain, code: ErrorNotExist, userInfo: [NSLocalizedDescriptionKey: "Dictionary[\"\(key)\"] does not exist"])
        }
      } else {
        returnJSON._error = self._error ?? NSError(domain: ErrorDomain, code: ErrorWrongType, userInfo: [NSLocalizedDescriptionKey: "Dictionary[\"\(key)\"] failure, It is not a dictionary"])
      }
      return returnJSON

The JSON file works with (pulled from a larger JSON file that validates in a validator):

          "color": {
            "red": 0.0,
            "green": 0.5,
            "blue": 0.5,
            "alpha": 0.5
          }

but if you remove the "red" entry you get that error with the Optional.

          "color": {
            "green": 0.5,
            "blue": 0.5,
            "alpha": 0.5
          }
@schurch
Copy link

schurch commented Jan 3, 2015

I'm seeing this too.

@skaugust
Copy link

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?

@axelrivera
Copy link

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"])
}

@dylan
Copy link

dylan commented Jan 17, 2015

+1

1 similar comment
@justinHowlett
Copy link

+1

@sbarex
Copy link

sbarex commented Mar 9, 2015

I found another possible patch for private subscript(#key: String) -> JSON explicity casting self.object as [String : AnyObject]:

            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"])
                }
            }

@LukeTangPL
Copy link
Member

#200

@sakthinath
Copy link

Hi,
I am using Swify Json latest update . In this I am facing Swifty Json Unwrapping Optional Issue Please Give any solution for this .Swift.ImplicitlyUnwrappedOptional<Swift.String>.some("1")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants