2015-04-04 84 views
0

我想將對象字典保存到iCloud,但是當我這樣做時saveToURL方法:forSaveOperation:completionHandler:失敗。我也試圖覆寫:用saveToURL保存到iCloud: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,即:

文件:///私人的/ var /移動/庫/移動% 20Documents/iCloud的〜COM〜〜資料inArrivo /停止/ favorite.dox

當我檢查調試器上的錯誤是:

錯誤域= NSCocoaErrorDomain代碼= 4「操作不能完成 。 (Cocoa error 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~information~inArrivo/Stops/favorite.dox, NSUnderlyingError = 0x17bfa860「操作無法完成。 (可可錯誤4。)」, NSURL =文件:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~information~inArrivo/Stops/favorite.dox}

如何我可以修復它,或者至少知道更多嗎?

回答

0

TSI Apple團隊回答了我,併爲我提供了一個工作班級。在檢查他們的版本後,這個變化似乎歸結爲 - (void)queryDidFinishGathering的更新: (NSNotification *)通知;加入:

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; 
}