将objectAtIndex解析为Array并保存NSUserDefaults

时间:2015-08-26 10:40:19

标签: ios parse-platform nsmutablearray nsarray nsuserdefaults

我有点被阻止我有解析数据我可以下载数据现在我想用NSUserDefaults保存这些数据或者其他因为我需要一个数组因为某些原因我只能看到第一个数据而不是全部

[super viewDidLoad];
PFQuery *query = [PFQuery queryWithClassName:@"DataStaff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
    for (NSObject *comment in comments)
    {
        NSString *name = [comment valueForKey:@"name"];
         NSLog(@"%@",name);


        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:name];
        [defaults setValue:data forKey:@"key"];

                    NSArray *array =@[@"%@",name];
                 [_textField setSuggestions:array];

来自我的控制台的信息

2015-08-26 12:31:35.547 RegisterED[17492:1760822] Internet IS OK
2015-08-26 12:31:38.654 RegisterED[17492:1760822] nsnsnsd
2015-08-26 12:31:38.664 RegisterED[17492:1760822] Honengar
2015-08-26 12:31:38.675 RegisterED[17492:1760822] Queytai
2015-08-26 12:31:38.682 RegisterED[17492:1760822] Endangurn
2015-08-26 12:31:38.686 RegisterED[17492:1760822] Endangurn
2015-08-26 12:31:38.688 RegisterED[17492:1760822] Chrilerwor
2015-08-26 12:31:38.691 RegisterED[17492:1760822] Anrayryn
2015-08-26 12:31:38.696 RegisterED[17492:1760822] Cryturage
2015-08-26 12:31:38.699 RegisterED[17492:1760822] Theipcha
2015-08-26 12:31:38.702 RegisterED[17492:1760822] Toimor
2015-08-26 12:31:38.705 RegisterED[17492:1760822] Restdan
2015-08-26 12:31:38.708 RegisterED[17492:1760822] Oldbser
2015-08-26 12:31:38.712 RegisterED[17492:1760822] Tonog
2015-08-26 12:31:38.717 RegisterED[17492:1760822] Saytinem


- (IBAction)read:(id)sender {

NSUserDefaults *defaultsFields = [NSUserDefaults standardUserDefaults];
NSArray *loadStrings = [defaultsFields stringArrayForKey:@"key"];
NSLog(@"%@", loadStrings);


}


This information from my console
Saytinem

1 个答案:

答案 0 :(得分:0)

您需要保存数组,然后使用NSUserDefaults构建NSMutableArray

// Initiliaze data
NSMutableArray *data = [[NSMutableArray alloc] init];

// Retrieve data
PFQuery *query = [PFQuery queryWithClassName:@"DataStaff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error)      {
    for (PFObject *comment in comments)
    {
        NSString *name = [comment valueForKey:@"name"];
        NSLog(@"%@",name);

        [data addObject:name];
    }

    // Save data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"key"];
    [defaults synchronize];
}];