将使用JSON解析的Twitter Feed中的数据保存到Plist中

时间:2012-06-22 19:03:06

标签: objective-c xcode uitableview plist

我有来自searchtwitter.com的Feed填充UITableView。我还有一个带有图像和UILabel的详细信息表。我已完成所有这些工作,但我想在详细信息页面上添加UIButton,将UIImage和标签保存到属性列表中。我对此非常陌生;这是我到目前为止(不工作)。

- (IBAction)SaveFriend:(id)sender
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [path objectAtIndex:0];

    NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"TwitterFriends.plist"]; 

    NSDictionary * tweet = [[NSDictionary alloc]init];

    NSString *text =  [tweet objectForKey:@"from_user"];
    NSString *user = [tweet objectForKey:@"from_user_name"];

    personName.text = text;
    personInfo.text = user;  

    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, personInfo, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"info", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData) 
    {
        // write plistData to our Data.plist file
        [tweet writeToFile:plistPath atomically:YES];
    }
    else 
    {
        NSLog(@"Error in saveData: %@", error);
       [error release];
    }
}

1 个答案:

答案 0 :(得分:0)

不使用.plist,而是尝试使用NSArray或NSMutableArray,然后使用writeToFile方法。例如:

NSMutableArray *temp = [NSMutableArray new];
[temp addObject:image];
[temp addObject:label];
[temp writeToFile:[self saveFilePath:@"filename"] atomically:YES];


//Returns the saved information path
- (NSString*) saveFilePath: (NSString *) add
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filename = [path stringByAppendingPathComponent:add];
    return filename;
}