Sharing a plist file using iCloud

前端 未结 2 1555
夕颜
夕颜 2021-02-06 08:13

I have a relativley simple app which persists data to a plist file located in the documents folder. The data loads into a UITableView at startup. The user can then edit, delete

2条回答
  •  猫巷女王i
    2021-02-06 08:36

    To use a plist with UIDocument, you can subclass UIDocument and override the following 2 methods with self.myDictionary (your plist) declared as a NSMutableDictionary.

    - (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError **)outError
    {    
        if ([contents length] > 0) 
        {
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:(NSData *)contents];
            NSMutableDictionary *dataDictionary = [unarchiver decodeObjectForKey:@"data"];
    
            self.myDictionary = dataDictionary;
            [unarchiver finishDecoding];
            [unarchiver release];
        } 
        else 
        {
            self.myDictionary =  [NSMutableDictionary dictionary];
        }
    
        return YES;    
    }
    
    - (id)contentsForType:(NSString *)typeName error:(NSError **)outError 
    {
        NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        if( !self.myDictionary )
        {
            self.myDictionary = [NSMutableDictionary dictionary];
        }
        [archiver encodeObject:self.myDictionary forKey:@"data"];
    
        [archiver finishEncoding];
        [archiver release];
        return data;
    }
    

提交回复
热议问题