[英]How to save data from parse to a NSArray iOS
如何以以下格式將數據從解析保存到NSArray
。 它用於填充UITableView
,該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沒有加載任何數據,當我通過NSLog
運行它時,它始終將值返回為null
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);
是否有將數據保存在上述類型的數組中的方法?
你的糖果從哪里來? 如果它是一個自定義類,則很可能不會保存為首先進行解析。
這是使用現有數據的一種方法,但是創建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]);
}
}];
然后,如果您想將它們保存回數組中,如您最初提出的問題
之所以為null的原因是
findObjectsInBackgroundWithBlock
該日志語句運行時尚未完成。 如果要處理結果(或記錄結果),則需要將log語句放入塊中,或者可以使用
findObjectsInBackgroundWithTarget:selector:
而是在您的回調方法中包含處理代碼。
我認為您尚未分配數組,因此請按以下方式更改代碼
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.