我正在尝试将对象字典保存到iCloud但是当我这样做时,方法saveToURL:forSaveOperation:completionHandler:failed。我也试图覆盖:
- (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;
}
当我记录要保存的网址时,即:
文件:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~information~inArrivo/Stops/favorite.dox
当我检查调试器上的错误时:
错误域= NSCocoaErrorDomain代码= 4“操作不能 完成。 (可可错误4.)“UserInfo = 0x17bd6cb0 {NSFileNewItemLocationKey =文件:///私人的/ var /移动/应用/ 445778BF-86AF-4DE3-9E1B-BAC8F79D14D0 / TMP /(A%20Document%20Being%20Saved%20By%20英寸%20Arrivo%20HD)/favorite.dox, NSFileOriginalItemLocationKey =文件:///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 :(得分: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;
}