Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

ios - Need to loop through an NSMutableArray that is stored inside an array in Parse.com database

I have an NSMutableArray object that contains NSString objects. The mutable array object is called _usersToAddToFriendsList. When I am done adding NSString objects to it, I run the following code for Parse.com:

[PFUser currentUser];

PFQuery *query = [PFQuery queryWithClassName:@"_User"];

[query whereKey:@"username" equalTo:_userSubmittedUsername];


[query getObjectInBackgroundWithId:_objectId block:^(PFObject *object, NSError *error) {


    object[@"friends"] = _usersToAddToFriendsList;

    [object saveInBackground];


}];

The most important part is this statement: object[@"friends"] = _usersToAddToFriendsList;

This takes my NSMutable array and places it inside the "friends" column in my Parse.com database. When I created this column in Parse, I set it's "type" to "array".

All of this works perfectly and I can see that the contents of my mutable array have been placed in the "friends" column in the database.

Here's the problem though. Later on, when I query for the "friends" column I use the method call of findObjectsInBackgroundWithBlock and this places the object returned by the server into an NSArray object called "objects".

My problem is that this means that I now have an array called "objects" that contains my original mutable array with my NSStrings.

I need to loop through the string values of my original NSMutableArray, but I don't know how to get to them because my original mutable array is contained inside this new NSArray returned by the server.

I have tried playing around with various multi-dimensional array solutions that people provided in my previous stack overflow question: Need to loop through an array that is inside of another array

But it never works and it always crashes in xcode and says:

-[PFUser countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance

This makes me think that this problem needs a solution specifically tailored to how Parse.com works.

I just want to be able access and loop through the string values that I originally stored in my NSMutableArray.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

My problem is that this means that I now have an array called "objects" that contains my original mutable array with my NSStrings.

Not quite. It means you have an array of user objects, each of which contains an array of friends (strings).

Try something like

for (PFUser *user in objects) {
    NSArray *friends = [user objectForKey:@"friends"];

    for (NSString *friend in friends) {
        NSLog(@"Friend is '%@'", friend);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...