当我分配 init NSMetaDataQuery 时,我的 applicationDidFinishLaunching 崩溃......请帮忙
我正在更新我的应用程序以使用 iCloud,并希望我的 iOS 部署目标保持为 3.2。我有条件地屏蔽任何 iOS 5 调用,以防止任何后台版本崩溃。我的 applicationDidFinishLaunching 看起来像...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(@"Launching Began...");
navigationController = [[UINavigationController alloc] init];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// Getting the documentsPath.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Setting up the mainCarList
//unarchive the saved carlist
NSString *archivePathFilename = [documentsPath stringByAppendingString:@"/carlist.archive"];
self.mainCarList = nil;
self.mainCarList = [[NSKeyedUnarchiver unarchiveObjectWithFile:archivePathFilename] retain];
//if OS version => 5.0 then
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
NSLog(@"in ApplicationDidFinishLaunching iOS version > 5.0");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(carListDocumentStateChanged) name:@"UIDocumentStateChangedNotification" object:Nil];
NSString *carListDocumentURLString = [documentsPath stringByAppendingString:@"/mainCarListDocument.CLD"];
self.mainCarListDocumentURL = [NSURL fileURLWithPath:carListDocumentURLString]; //sets the local save location only in this property
Class cls = NSClassFromString (@"NSMetadataQuery");
if (cls) {
NSMetadataQuery *query;
query = [[NSMetadataQuery alloc] init]; // <------- This crashes when running v4.3 iPhone sim.
/*
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[self setDocumentMetaDataQuery:query];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDataScope]];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K == %@", NSMetadataItemFSNameKey, @"mainCarListDocument.CLD"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering) name:NSMetadataQueryDidFinishGatheringNotification object:Nil];
BOOL didStart = NO;
didStart = [query startQuery];
if (didStart) {
NSLog(@"documentMetaDataQuery started");
}
else {
NSLog(@"error starting documentMetaDataQuery");
}
*/
[query release];
}
...
我认为编译时发生了一些事情,因为“启动开始”日志没有发布。条件块工作正常。如果我注释掉查询的 alloc init,则仅当 iOS 5 正在运行时该块才会运行。有什么想法吗?
I am updating my app to use iCloud and want my iOS deployment target to remain 3.2. I am conditionally shielding any iOS 5 calls to prevent any back level versions from crashing. My applicationDidFinishLaunching looks like...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(@"Launching Began...");
navigationController = [[UINavigationController alloc] init];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// Getting the documentsPath.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Setting up the mainCarList
//unarchive the saved carlist
NSString *archivePathFilename = [documentsPath stringByAppendingString:@"/carlist.archive"];
self.mainCarList = nil;
self.mainCarList = [[NSKeyedUnarchiver unarchiveObjectWithFile:archivePathFilename] retain];
//if OS version => 5.0 then
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
NSLog(@"in ApplicationDidFinishLaunching iOS version > 5.0");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(carListDocumentStateChanged) name:@"UIDocumentStateChangedNotification" object:Nil];
NSString *carListDocumentURLString = [documentsPath stringByAppendingString:@"/mainCarListDocument.CLD"];
self.mainCarListDocumentURL = [NSURL fileURLWithPath:carListDocumentURLString]; //sets the local save location only in this property
Class cls = NSClassFromString (@"NSMetadataQuery");
if (cls) {
NSMetadataQuery *query;
query = [[NSMetadataQuery alloc] init]; // <------- This crashes when running v4.3 iPhone sim.
/*
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[self setDocumentMetaDataQuery:query];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDataScope]];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K == %@", NSMetadataItemFSNameKey, @"mainCarListDocument.CLD"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering) name:NSMetadataQueryDidFinishGatheringNotification object:Nil];
BOOL didStart = NO;
didStart = [query startQuery];
if (didStart) {
NSLog(@"documentMetaDataQuery started");
}
else {
NSLog(@"error starting documentMetaDataQuery");
}
*/
[query release];
}
...
I think something is going on at compile time since the "Launching Began" log is not posted. The conditional block works correctly. If I comment out the query's alloc init, then the block gets ran only if iOS 5 is running. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换以下代码:
通过此代码:
Replace the following code:
By this code:
您必须使用 cls 而不是 NSMetadataQuery。
You have to use cls instead of NSMetadataQuery.