我尝试使用CoreData来保留一些数据。数据从url下载,它是一个包含一组用户详细信息的Json。我尝试解析json并将数据转换为我的NSManagedObjectModel子类,称为" User"。但是,应用程序中断没有任何解释。任何人都可以告诉我我的代码有什么问题吗?
//var friend = [User]()
func loadUpdateData(){
self.refreshControl?.beginRefreshing()
let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as NSString
let url = NSURL( string: "http://192.168.0.101/index.php?method=updateData&id=1" )
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 10)
request.HTTPMethod = "POST"
request.HTTPBody = checkList!
var updateQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: updateQueue, completionHandler: { (response, recievedData, error) -> Void in
if error == nil && (recievedData != nil) {
let dataString = NSString(data: recievedData, encoding: NSUTF8StringEncoding)
let readableData = dataString?.dataUsingEncoding(NSUTF8StringEncoding)
var updatedFriendList = NSJSONSerialization.JSONObjectWithData(readableData!, options: NSJSONReadingOptions.MutableContainers, error: nil)! as [NSDictionary]
dispatch_async(dispatch_get_main_queue(), {
for (var index=0; index<updatedFriendList.count; index++) {
User.insertNewUser(self.objectContext!, newUserDict: updatedFriendList[index]) **//it seems app crashes at this line is being executed.**
}
self.objectContext?.save(nil)
self.refreshControl?.endRefreshing()
})
}else{
dispatch_async(dispatch_get_main_queue(), {
let _ = self.refreshControl?.endRefreshing()
})
}
})
}
用于&#34;用户&#34;类:
class User: NSManagedObject {
@NSManaged var userName: String
@NSManaged var userNickName: String
@NSManaged var sex: NSNumber
@NSManaged var birth: NSDate
@NSManaged var userID: NSNumber
@NSManaged var userAvatar: NSData
@NSManaged var numOfTarget: NSNumber
@NSManaged var latestUpdateTime: NSDate
class func insertNewUser(moc: NSManagedObjectContext, newUserDict: NSDictionary) {
let user = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: moc) as User
user.userID = newUserDict.valueForKey("userID") as NSNumber
user.userName = newUserDict.valueForKey("userName") as String
user.latestUpdateTime = newUserDict.valueForKey("latestUpdateTime") as NSDate
user.numOfTarget = newUserDict.valueForKey("numOfTarget") as NSNumber
user.userNickName = newUserDict.valueForKey("userNickName") as String
user.userAvatar = newUserDict.valueForKey("userAvatar") as NSData
user.sex = newUserDict.valueForKey("sex") as NSNumber
user.birth = newUserDict.valueForKey("birth") as NSDate
moc.save(nil)
}
- 更新错误快照 - libsystem_kernel.dylib`__pthread_kill: 0x19317b268:movz x16,#328 0x19317b26c:svc#128 0x19317b270:b.cc 0x19317b288; __pthread_kill + 32 0x19317b274:stp fp,lr,[sp,# - 16]! 0x19317b278:mov fp,sp 0x19317b27c:bl 0x19316259c; cerror_nocancel 0x19317b280:mov sp,fp 0x19317b284:ldp fp,lr,[sp],#16
2015-04-09 22:47:27.488 Incentive [355:78244] - [Incentive.User setNumOfTarget:]:无法识别的选择器发送到实例0x1702c1c70 2015-04-09 22:47:27.492奖励[355:78244] *由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; - [Incentive.User setNumOfTarget:]:无法识别的选择器发送到实例0x1702c1c70&#39; * 第一次抛出调用堆栈: (0x18229e59c 0x1929f40e4 0x1822a5664 0x1822a2418 0x1821a6b6c 0x1000a2678 0x10009b79c 0x10009c294 0x181c86f34 0x183157508 0x1830a8c94 0x18309861c 0x18315a26c 0x1004e0df0 0x1004eb854 0x1004e4120 0x1004ed75c 0x1004eef18 0x1932152e4 0x193214fa8) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)
答案 0 :(得分:0)
这是解释:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Incentive.User setNumOfTarget:]: unrecognized selector sent to instance 0x1702c1c70'
这意味着User类不了解setNumOfTarget:。检查您的CoreData模型是否实际正确定义了它。