gpt4 book ai didi

objective-c - EXC_BAD_ACCESS 在多个设备上使用 iCloud

转载 作者:可可西里 更新时间:2023-11-01 06:11:51 25 4
gpt4 key购买 nike

我正在使用 iCloud 创建一个应用程序。但我有一些问题。它使用 NSFileWrapper 在 iCloud 上创建目录,然后在 NSFileWrapper 目录中创建 NSData(容器)文件。我正在使用此代码将 NSFileWrapper 转换为 NSMutableArray:

NSFileWrapper *MyWrapper=[[[MyDocument data] fileWrappers] objectForKey:@"myFile.doh"];
    NSData *MyData=[NSData dataWithData:[MyWrapper regularFileContents]];
    NSMutableArray *MyList=[NSPropertyListSerialization propertyListFromData:MyData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil];

而且它只在创建了这个容器的设备上才能正常工作。在其他设备上,这段代码的结果是 BAD_ACCESS(在代码的第二行,我开始用数据做一些事情)。调试时,函数“regularFileContents”返回具有正确数据大小的正确对象,但当我尝试读取此数据时,发生 BAD_ACEESS(code=10)。

我用的是ARC,所以不是内存管理的错误。

问题可能出在某些项目/代码签名设置中?有什么想法吗?

谢谢!

最佳答案

我也遇到过这个问题,经过大量实验后我发现,即使外包装已经下载,但内部内容实际上还没有下载,这导致对 regularFileContents 的调用失败。

我一直在 MyWrapper 上调用 startDownloadingUbiquitousItemAtURL,一旦完成,错误就消失了。这是一种检查文件下载状态的方法(假设您知道 MyWrapper 的 url)并在尚未下载时开始下载。

-(BOOL)downloadFileIfNotAvailable:(NSURL*)fileURL
{
NSNumber *isInCloud = nil;

if ([fileURL getResourceValue:&isInCloud forKey:NSURLIsUbiquitousItemKey error:nil])
{
if ([isInCloud boolValue]) {
NSNumber *isDownloaded = nil;
if ([fileURL getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil])
{
if ([isDownloaded boolValue])
{
return YES;
}

NSError *error = nil;
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:fileURL error:&error];

if (error)
{
NSLog(@"Download Failed :: %@", error);
}

return NO;
}
}
}

return YES;
}

关于objective-c - EXC_BAD_ACCESS 在多个设备上使用 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com