简体   繁体   中英

Parsing a JSON retrieved int32 into a core data class with swift

I have a Core Data class which is being populated via a JSON rest API call using Swift.

I can parse the dictionary without any problems for strings, but cannot parse the ID field which is an Int32.

My code is:

class Job: NSManagedObject {
    @NSManaged var id: Int32
}


if let id = resultDict["JobID"] as? NSNumber {
    job.id = Int32(id) // THIS LINE IS CAUSING THE ERROR 
}

The error that I'm getting when trying to build is: 'Could not find an overload for 'init' that accepts the supplied arguments"

Int32 doesn't have an initializer that takes an NSNumber . You can use the intValue property of NSNumber directly though; it returns an Int32 :

job.id = id.intValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM