如何将数据从解析保存到NSArray iOS

时间:2014-01-31 09:42:27

标签: ios objective-c nsarray parse-platform pfquery

如何以下面的格式将数据从解析保存到NSArray。用于填充UITableView,用于搜索栏。

_candyArray = [NSArray arrayWithObjects:
                  [Candy candyOfCategory:@"chocolate" name:@"chocolate bar" mName:@"test1"],
                  [Candy candyOfCategory:@"chocolate" name:@"chocolate chip" mName:@"test1"],
                  [Candy candyOfCategory:@"chocolate" name:@"dark chocolate" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"lollipop" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"candy cane" mName:@"test1"],
                  [Candy candyOfCategory:@"hard" name:@"jaw breaker" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"caramel" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"sour chew" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"peanut butter cup" mName:@"test1"],
                  [Candy candyOfCategory:@"other" name:@"gummi bear" mName:@"test1"], nil];

我尝试了下面的代码,但是ID没有加载任何数据,当我通过null

运行时,将值保持为NSLog
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"Name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        {
            self.arrayName = objects;
        }   
    }else {
        NSLog(@"Error, %@ %@",error,[error userInfo]);
    }
}];

NSLog(@"Array : %@", arrayName);

是否有将数据保存在上述类型的数组中?

3 个答案:

答案 0 :(得分:0)

你的糖果来自哪里?如果它是一个自定义类,那么它可能不会保存到首先解析。

这是使用现有数据的一种方法,但也许最好创建一个PFObject子类。 (检查文档)

NSMutableArray * candyObjects = [NSMutableArray alloc]init];

for (Candy * candy in _candyArray) {   
    PFObject * newCandy = [PFObject objectWithClassName:@"Candy"];
    newCandy[@"category"] = candy.category; // or however you store your candy object's properties
    newCandy[@"name"] = candy.name; // I prefer lowercase names, I notice you're querying uppercase
    newCandy[@"mName"] = candy.mName;

    [candyObjects addObject:newCandy];
}

[PFObject saveAll:candyObjects]; // should run in background, but for now just showing to save.

好的,下一步是检索它们。 (基本上rajjjjj的例子有一些变化)

PFQuery *query = [PFQuery queryWithClassName:@"Candy"];
[query orderByAscending:@"name"]; // lowercase to match above
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"retrievedObjects:%@", objects);

        // Now to get them back into an array like you wanted before
        // again, perhaps you could skip this by subclassing your candy ob

        for (PFObject * obbie in objects) {
            // mutableArrayRef represents wherever you'd like your candies added.
            // make sure it has memory allocated, and it has been initialized
            [mutableArrayRef addObject:[Candy candyOfCategory:obbie[@"category"] name:obbie[@"name"] mName:obbie[@"mName"]]];
        }

        // now continue in whatever way you please, all of your candy should be in mutableArrayRef
        // if you're doing multiple searches, make sure you clear the array before adding
        // otherwise you will stack the data and get undesired results
    }
    else {
        NSLog(@"Error, %@ %@",error,[error userInfo]);
    }
}];

然后,如果你想将它们保存回原来陈述的问题

答案 1 :(得分:0)

它为null的原因是

findObjectsInBackgroundWithBlock

运行该日志语句时

未完成。如果要处理结果(或记录它们),则需要将log语句放在块中,或者可以使用

findObjectsInBackgroundWithTarget:selector:

代替,并在回调方法中使用处理代码。

答案 2 :(得分:-1)

你还没有分配数组,所以改变你的代码如下

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"Name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
    {
        self.arrayName = [[NSArray alloc]initwithArray:objects];
    }   
}else {
    NSLog(@"Error, %@ %@",error,[error userInfo]);
}
}];

NSLog(@"Array : %@", self.arrayName);