需要遍历存储在 Parse.com 数据库中的数组中的 NSMutableArray [英] Need to loop through an NSMutableArray that is stored inside an array in Parse.com database

查看:49
本文介绍了需要遍历存储在 Parse.com 数据库中的数组中的 NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 NSString 对象的 NSMutableArray 对象.可变数组对象称为_usersToAddToFriendsList.当我完成向它添加 NSString 对象后,我为 Parse.com 运行以下代码:

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


}];

最重要的部分是这个语句:object[@"friends"] = _usersToAddToFriendsList;

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

这需要我的 NSMutable 数组并将它放在我的 Parse.com 数据库的朋友"列中.当我在 Parse 中创建此列时,我将它的类型"设置为数组".

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.

问题来了.稍后,当我查询friends"列时,我使用了 findObjectsInBackgroundWithBlock 的方法调用,这会将服务器返回的对象放入一个名为objects"的 NSArray 对象中.

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".

我的问题是,这意味着我现在有一个名为objects"的数组,其中包含我的原始可变数组和我的 NSStrings.

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

我需要遍历原始 NSMutableArray 的字符串值,但我不知道如何获取它们,因为原始可变数组包含在服务器返回的这个新 NSArray 中.

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

但它永远不会工作,它总是在 xcode 中崩溃并说:

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

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

这让我觉得这个问题需要一个专门针对 Parse.com 的工作方式量身定制的解决方案.

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

我只想能够访问和循环遍历我最初存储在 NSMutableArray 中的字符串值.

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

推荐答案

我的问题是,这意味着我现在有一个名为objects"的数组,其中包含我的原始可变数组和我的 NSStrings.

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

尝试类似的东西

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

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

这篇关于需要遍历存储在 Parse.com 数据库中的数组中的 NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆