Must I explicitely enable exceptions in Xcode?

自古美人都是妖i 提交于 2020-01-06 07:21:15

问题


-objectWithID: is supposed to give me an object that's broken when the ID doesn't exist. The documentation says, that this object throws an exception when I try to access an property.

However, it never throws any. Must I enable exceptions so that they're really thrown?

Here's some code:

// Assume: a new managed object has been created. Then it's ID has been converted to NSURL.
// The MO has been saved. Then the NSURL has been converted back to an NSManagedObjectID *tmpID
// So tmpID is an ID that doesn't exist anymore, since the ID of the MO has changed due to persisting it
@try {
    NSManagedObject *mo = [context objectWithID:tmpID]; // tmpID doesnt exist anymore!
    NSString *timeStamp = [[mo valueForKey:@"timeStamp"] description]; // nil
    [mo setValue:[NSDate date] forKey:@"timeStamp"];
}
@catch (NSException * e) {
    NSLog(@"Error: %@: %@", [e name], [e reason]); // never called
}

回答1:


How are you testing this? Core Data projects tend to have exceptions turned on by default.

What does your test look like?

Update

How do you know that tempID does not exist? Can you show the code that creates that tempID?

after saving, a tmpID turns into a permanent id, right? ...so when I convert it to NSURL to keep it around, and then convert it back after saving, it should be invalid...or not?

There is no guarantee of that. It would not surprise me if Core Data kept a lookup from the temp to the permanent for a while.

If you really want to see it throw an exception, change that temp to something else and throw that at Core Data. If you still don't get an exception after that then it is time to file a radar.



来源:https://stackoverflow.com/questions/3013194/must-i-explicitely-enable-exceptions-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!