简体   繁体   中英

NSArray value from PFObject

How can load NSArray value from saved PFOBject?

Save:

NSMutableArray *arr = [[NSMutableArray alloc]init];

   for (MyParseInformation *obj in results) {
       [arr addObject:obj.correct]; // correct is NSNumber
    }
    [pfobject addObject:arr forKey:@"results"];
}

[pfobject saveEventually:^(BOOL succeeded, NSError *error) {

Query:

PFQuery *query = [PFQuery queryWithClassName:className];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if(!error){
         for (PFObject *obj in objects) {

            id str = [obj valueForKey:@"results"] ;

...... id is __NSArrayM type class with count=1 and its content is:

(
        (
        1,
        1,
        1,
        1,
        1,
        1,
        0,
        0,
        0,
        1,
        1,
        1,
        0,
        1
    )
) 

Your str object is an NSArray* that contains one object, which itself is an NSArray* that is presumably the value you want.

I'm not familiar with PFObject but I'm going to guess that -addObject:forKey: stores the objects that are added in an array associated with the key. Since you only added one object, the resulting array has only one value in it. You probably wanted to use -setObject:forKey: instead.

Kevin is correct. -[PFObject addObject:forKey:] is used for inserting new elements into an array key. This means you inserted an array into an array. addObject:forKey: is useful when that object's key might be subject to race conditions, ie if you had a comments array on a forum object, [post addObject:aComment forKey:@"comments"] ensures that all comments will be saved when two clients are racing to change the value of @"comments". This would not be the case if I had done something like [post setObject:@[aComment] forKey:@"comments"] .

You could have written your code as:

for (MyParseInformation *obj in results) {
   [pfobject addObject:obj.correct]; // correct is NSNumber
}

but in your case, it might be just as easy to replace your addObject:forKey: line with setObject:forKey:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM