当我分配 init NSMetaDataQuery 时,我的 applicationDidFinishLaunching 崩溃......请帮忙

发布于 2024-12-11 04:41:17 字数 2711 浏览 3 评论 0原文

我正在更新我的应用程序以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-12-18 04:41:17

替换以下代码:

NSMetadataQuery *query;
query = [[NSMetadataQuery alloc] init];

通过此代码:

id query;
query = [[cls alloc] init];

Replace the following code:

NSMetadataQuery *query;
query = [[NSMetadataQuery alloc] init];

By this code:

id query;
query = [[cls alloc] init];
幸福不弃 2024-12-18 04:41:17

您必须使用 cls 而不是 NSMetadataQuery。

You have to use cls instead of NSMetadataQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文