简体   繁体   English

快速将JSON检索的int32解析为核心数据类

[英]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. 我有一个Core Data类,正在使用Swift通过JSON rest API调用进行填充。

I can parse the dictionary without any problems for strings, but cannot parse the ID field which is an Int32. 我可以解析字典,而字符串没有任何问题,但是不能解析ID字段,它是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" 我在尝试构建时遇到的错误是:“无法找到接受提供的参数的'init'的重载”

Int32 doesn't have an initializer that takes an NSNumber . Int32没有带有NSNumber的初始化程序。 You can use the intValue property of NSNumber directly though; 您可以直接使用NSNumberintValue属性 it returns an Int32 : 它返回一个Int32

job.id = id.intValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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