0

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);

上記のタイプの配列にデータを保存する方法はありますか?

4

3 に答える 3

0

null である理由は、

findObjectsInBackgroundWithBlock

そのログ ステートメントが実行されたときに終了していません。結果を処理する (またはログに記録する) 場合は、ログ ステートメントをブロック内に配置するか、次を使用できます。

findObjectsInBackgroundWithTarget:selector:

代わりに、コールバック メソッドに処理コードを含めます。

于 2014-03-13T10:31:06.560 に答える
0

あなたのキャンディーオブジェクトはどこから来ていますか? カスタムクラスの場合、そもそも解析するために保存されていない可能性があります。

これは既存のデータを使用する 1 つの方法ですが、おそらく 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]);
    }
}];

次に、質問が最初に述べたように、それらを配列に保存したい場合

于 2014-01-31T22:10:36.207 に答える
-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);
于 2014-01-31T09:58:07.727 に答える