Ios 使用saveToURL:forSaveOperation:completionHandler:保存到iCloud失败

Ios 使用saveToURL:forSaveOperation:completionHandler:保存到iCloud失败,ios,icloud,nserror,Ios,Icloud,Nserror,我正试图将对象字典保存到iCloud,但当我这样做时,方法saveToURL:forSaveOperation:completionHandler:失败。我还试图覆盖: - (BOOL)writeContents:(id)contents andAttributes:(NSDictionary *)additionalFileAttributes safelyToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperati

我正试图将对象字典保存到iCloud,但当我这样做时,方法saveToURL:forSaveOperation:completionHandler:失败。我还试图覆盖:

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError
当然超级呼叫也返回false。然而,我本想读取错误,但当我尝试使用localizedError时,编译器报告一个错误,声称它不是一个结构或联合。 这是完整的代码:

-(instancetype)initWithSingleton{
NSURL *ubiq = [[NSFileManager defaultManager]
               URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:
                             @"Stops"] URLByAppendingPathComponent:kFILENAME];
NSLog(@"file url=%@", ubiquitousPackage);
self=[self initWithFileURL:ubiquitousPackage];
if (self!=nil){
    self.favoriteStops=[[NSMutableDictionary alloc] init];
    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"iCloud access at %@", ubiq);
        [self loadDocument];
    } else {
        NSLog(@"No iCloud access");
    }
}
return self;
}
#define kFILENAME @"favorite.dox"

- (void)loadData:(NSMetadataQuery *)query {

if ([query resultCount] == 1) {
    NSMetadataItem *item = [query resultAtIndex:0];
    NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSData *GETReply= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSMutableDictionary* dict=[NSKeyedUnarchiver unarchiveObjectWithData:GETReply];
    [self setFavoriteStops:dict];
    NSLog(@"favorites: %@", favoriteStops);
    [self openWithCompletionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"iCloud document opened");
        } else {
            NSLog(@"failed opening document from iCloud");
        }
    }];
}
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:NSMetadataQueryDidFinishGatheringNotification
                                              object:query];

_query = nil;
[self loadData:query];

}
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName
               error:(NSError **)outError
{
if ([contents length] > 0) {
    [self setFavoriteStops:[NSKeyedUnarchiver unarchiveObjectWithData:contents]];
}
return YES;
}

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError{
//logging
NSString *str;
str= [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
NSLog(@"saving data %@", str);
//logging
NSMutableDictionary *dict=[NSKeyedUnarchiver unarchiveObjectWithData:contents];
NSLog(@"dict=%@", dict);

BOOL success= [super writeContents:contents
        andAttributes:additionalFileAttributes
          safelyToURL:url
     forSaveOperation:saveOperation
                error:outError];
NSLog(@"error :%@", outError.localizedDescription) //syntax error
return success;
}

-(void) save{
NSLog(@"file url=%@", [self fileURL]);
[self saveToURL:[self fileURL]
forSaveOperation:UIDocumentSaveForOverwriting
completionHandler:^(BOOL success) {
  if (success) { //this returns false
      [self openWithCompletionHandler:^(BOOL success) {
          NSLog(@"new document saved on iCloud");
      }];
  } else {
      NSLog(@"error in iCloud Saving");
  }
 }];
}

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
NSLog(@"favorite stops=%@ class=%@", self.favoriteStops, [favoriteStops class]);
NSData *archivedData=[NSKeyedArchiver archivedDataWithRootObject:self.favoriteStops];
return archivedData;

}
当我记录要保存的url时,即:

file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~信息~inArrivo/Stops/favorite.dox

当我在调试器上检查错误时,它是:

错误域=NSCOCAERRORDOMAIN Code=4无法执行该操作 完整的。错误4。UserInfo=0x17bd6cb0 {NSFileNewItemLocationKey=file:///private/var/mobile/Applications/445778BF-86AF-4DE3-9E1B-BAC8F79D14D0/tmp/A%20Document%20Being%20Saved%20By%20In%20Arrivo%20HD/favorite.dox, NSFileOriginalItemLocationKey=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~信息~inArrivo/Stops/favorite.dox, NSUnderlyingError=0x17bfa860无法完成该操作。 Cocoa错误4。, NSURL=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~信息~inArrivo/Stops/favorite.dox}


我该如何修复它,或者至少了解更多信息?

TSI苹果团队回答了我的问题,并为我提供了一个工人阶级。在与我核对他们的版本后,更改似乎归结为对-voidqueryDidFinishGathering:NSNotification*notification的更新;加入:

if (query.results == 0)
    {
        [self save];  // no favorites file, so create one
    }
具体如下:

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:NSMetadataQueryDidFinishGatheringNotification
                                              object:query];
//•• added
if (query.results == 0)
    {
        [self save];  // no favorites file, so create one
    }

   [self loadData:query];

   _query = nil;
}