0
我想我可能會瘋了。在下面的方法中,返回值_persistentStoreCoordinator是零除非我添加另一行代碼。檢查_persistentStoreCoordinator == nil
就足以確保它不是。 (一條NSLog語句也可以做到這一點)。對象零,除非我添加另一行'隨機'代碼
如果我不添加另一行,_persistentStoreCoordinator在該方法的最後一行中爲零,即使使用中斷點檢查psc
始終爲非零。
最奇怪的(也許最有用的?)的事情是,當問題開始時,我沒有對這個類做任何改變。
任何幫助或解釋非常感謝!
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator == nil) {
NSLog(@"SQLITE STORE PATH: %@", [self pathToLocalStore]);
NSURL *storeURL = [NSURL fileURLWithPath:[self pathToLocalStore]];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *e = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&e]) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:e forKey:NSUnderlyingErrorKey];
NSString *reason = @"Could not create persistent store.";
NSException *exc = [NSException exceptionWithName:NSInternalInconsistencyException
reason:reason
userInfo:userInfo];
@throw exc;
}
_persistentStoreCoordinator = psc;
if (_persistentStoreCoordinator == nil)
{
NSLog(@"We never reach here.");
}
}
return _persistentStoreCoordinator;
}
有時調試器顯示一個對象爲零時,它的acutally不是。在這種情況下,用nslogging幫助你自己。您可以直接記錄對象。 NSLog(「myObject:%@」,_ persistentStoreCoordinator);這樣做你至少會看到它的地址或零。 –
嗨赫爾曼,謝謝我試過。奇怪的是,如果我像這樣放置它: '... _persistentStoreCoordinator = psc; NSLog(@「myObject:%@」,_ persistentStoreCoordinator); } return _persistentStoreCoordinator; }' 它工作。但是,在if語句完成後放置相同的語句: '... _persistentStoreCoordinator = psc; (@「myObject:%@」,_ persistentStoreCoordinator); return _persistentStoreCoordinator; }' 最終返回空值。 –
在OP中,你說if(){NSLog()}使得返回值不是'nil',並且如果你添加了另一個引用該對象的NSLog,它會停止工作? –