错误:[__NSCFConstantString 路径]:发送了无法识别的选择器
Posted
技术标签:
【中文标题】错误:[__NSCFConstantString 路径]:发送了无法识别的选择器【英文标题】:Error: [__NSCFConstantString path]: unrecognized selector sent 【发布时间】:2012-11-12 18:12:30 【问题描述】:我正在尝试实现 Core Data 与 iCloud 的同步。
当我尝试 [self fetchedResultsControllerICloud] performFetch:&error] in
-(id)init
[self managedObjectModelICloud];
[self managedObjectContextICloud];
if (![[self fetchedResultsControllerICloud] performFetch:&error])
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
我有一个错误
2012-11-12 23:50:34.917 My English words[1072:907] iCloud access at file://localhost/private/var/mobile/Library/Mobile%20Documents/LUB2V2L4R3~ru~________~My-English-words/
2012-11-12 23:50:34.920 My English words[1072:907] count: 0
2012-11-12 23:50:34.935 My English words[1072:110b] iCloudData: /private/var/mobile/Library/Mobile Documents/LUB2V2L4R3~ru~________~My-English-words/Documents/My_English_words.sqlite
2012-11-12 23:50:34.938 My English words[1072:110b] -[__NSCFConstantString path]: unrecognized selector sent to instance 0x8ca28
2012-11-12 23:50:34.940 My English words[1072:110b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString path]: unrecognized selector sent to instance 0x8ca28'
*** First throw call stack:
(0x331012a3 0x33fbf97f 0x33104e07 0x33103531 0x3305af68 0x344b840b 0x343805cf 0x3437e73d 0x7fe33 0x356f111f 0x356ff259 0x356ff3b9 0x3572fa11 0x3572f8a4)
libc++abi.dylib: terminate called throwing an exception
(lldb)
与 [_persistentStoreCoordinatorICloud addPersistentStoreWithType:NSSQLiteStoreType 字符串 在 - (NSPersistentStoreCoordinator *)persistentStoreCoordinatorICloud 方法中
#define sqlLiteDataBaseName @"My_English_words.sqlite"
#define sqlLiteDataBasePath @"Documents"
#define iCloudEnabledAppID @"LUB2V2L4R3.ru.________.My-English-words"
//************************************************
- (NSManagedObjectContext *)managedObjectContextICloud
if (_managedObjectContextICloud != nil)
return _managedObjectContextICloud;
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinatorICloud];
if (coordinator != nil)
_managedObjectContextICloud = [[NSManagedObjectContext alloc] init];
[_managedObjectContextICloud setPersistentStoreCoordinator:coordinator];
return _managedObjectContextICloud;
//************************************************
- (NSManagedObjectModel *)managedObjectModelICloud
if (_managedObjectModelICloud != nil)
return _managedObjectModelICloud;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"My_English_words" withExtension:@"momd"];
_managedObjectModelICloud = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModelICloud;
//************************************************
- (NSPersistentStoreCoordinator *)persistentStoreCoordinatorICloud
NSURL *iCloud = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSLog(@"iCloud access at %@", iCloud);
NSString *pathString=[[iCloud path] stringByAppendingPathComponent:sqlLiteDataBasePath];
_persistentStoreCoordinatorICloud = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModelICloud]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
if([[NSFileManager defaultManager] fileExistsAtPath:pathString] == NO)
NSLog(@"!Exist");
NSError *fileSystemError;
[[NSFileManager defaultManager] createDirectoryAtPath:pathString
withIntermediateDirectories:YES
attributes:nil
error:&fileSystemError];
if(fileSystemError != nil)
NSLog(@"Error creating database directory %@", fileSystemError);
NSString *iCloudData = [[[iCloud path] stringByAppendingPathComponent:sqlLiteDataBasePath] stringByAppendingPathComponent:sqlLiteDataBaseName];
NSLog(@"iCloudData: %@",iCloudData);
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[options setObject:iCloudEnabledAppID forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:sqlLiteDataBasePath forKey:NSPersistentStoreUbiquitousContentURLKey];
[_persistentStoreCoordinatorICloud lock];
[_persistentStoreCoordinatorICloud addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[NSURL fileURLWithPath:iCloudData]
options:options
error:nil];
[_persistentStoreCoordinatorICloud unlock];
dispatch_async(dispatch_get_main_queue(), ^
NSLog(@"asynchronously added persistent store!");
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil];
);
);
return _persistentStoreCoordinatorICloud;
//************************************************
- (NSFetchedResultsController *)fetchedResultsControllerICloud
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"OriginalWords" inManagedObjectContext:_managedObjectContextICloud];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:1000];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_managedObjectContextICloud
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsControllerICloud = theFetchedResultsController;
_fetchedResultsControllerICloud.delegate = self;
return _fetchedResultsControllerICloud;
【问题讨论】:
某处你有一个 NSString 可能应该有一个 NSURL。如果您查看异常回溯,它会告诉您在哪里。 我怀疑iCloud
变量没有被正确地带入块上下文中。如果您在块内调用URLForUbiquityContainerIdentifier:
方法而不是使用iCloud
会怎样? (如果它消除了错误消息,它会缩小问题范围。)
感谢您的建议,真的很有趣,尽管有这个错误,运行这个脚本后,sqlite 文件是在 iCloud 中创建的。
错误已解决。问题出在 [options setObject:sqlLiteDataBasePath forKey:NSPersistentStoreUbiquitousContentURLKey]; #define sqlLiteDataBasePath @"Documents" 非常愚蠢的错误,但现在我遇到了另一个错误:'这个 NSPersistentStoreCoordinator 没有持久存储。它无法执行保存操作”。我认为这是由于 [_managedObjectContextICloud save:&error]
【参考方案1】:
错误已解决。问题出在
[options setObject:sqlLiteDataBasePath forKey:NSPersistentStoreUbiquitousContentURLKey];
sqlLiteDataBasePath路径应该是完整的
[[iCloud URLByAppendingPathComponent:sqlLiteDataBasePath] URLByAppendingPathComponent:sqlLiteDataBaseName]
非常愚蠢的错误,但现在我又遇到了一个错误:
'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation'.
我认为这是由于
[_managedObjectContextICloud save:&error]
【讨论】:
解决了!答案是:您也可能做错了修改上下文(例如向其中添加新的托管对象),然后在存储初始化之前尝试保存它。以上是关于错误:[__NSCFConstantString 路径]:发送了无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章
[__NSCFConstantString objectAtIndex:]:无法识别的选择器发送到实例 0x3141c
如何测试 NSMutableArray 中是不是存在 _NSCFConstantString?
NSInvalidArgumentException',原因:'-[__NSCFConstantString objectForKey:]:无法识别的选择器发送到实例 0x10256a1b0'