我希望在PFObjet
内保存值,并将其保存到多个NSMutableArray
。让我解释。我从Parse中的Partisipants
类下载信息。 Partisipants
类包含多个NSString
值,例如name
,phoneNumber
等。我希望从查询的返回值中删除该信息,并将新objectID
的每个实例添加到NSMutableArray
。假设有多行,或userID
来搜索NSMutableArray
。
PFQuery *query = [PFQuery queryWithClassName:@"Partisipants"];
[query setLimit: 600];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded. The first 600 objects are available in objects
self.objectsFromParse = objects;
NSLog(@"%@", self.objectsFromParse);
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
修改
这是我执行此查询的返回结果(个人信息已被修改):
(
"<Partisipants:S420zHiNFT:(null)> {\n additionalDonation = \"100.00\";\n emailAddress = \"somewhere@email.com\";\n emergencyPhone = 12345678901;\n hasArrived = NO;\n name = \"James\";\n phoneNumber = 12345678901;\n}",
"<Partisipants:9BpoFxO1zx:(null)> {\n additionalDonation = \"500.00\";\n emailAddress = \"someguy@email.com\";\n emergencyPhone = 12345678901;\n hasArrived = NO;\n name = \"Michael\";\n phoneNumber = 12345678901;\n}"
)
答案 0 :(得分:1)
如果你想在新的mutableArrays中存储电子邮件,名字,objectId等,你可以这样做:
NSMutableArray * usernames = [NSMutableArray array];
NSMutableArray * emails = [NSMutableArray array];
NSMutableArray * objectIds = [NSMutableArray array];
for (PFObject * participant in objects) {
if (participant[@"name"]) [usernames addObject:participant[@"name"]];
if (participant[@"emailAddress"]) [emails addObject:participant[@"emailAddress"]];
[objectIds addObject:participant.objectId];
}
答案 1 :(得分:0)
创建参与者数组。然后循环遍历它们并构建可变数组(nameArray,email array等)。请注意,如果某些参与者数据是可选的,这是一种非常糟糕的方法。例如,您如何表示没有电子邮件地址?你不能在数组中使用nil,所以你必须使用NSNull或空字符串来表示它......
答案 2 :(得分:0)
您可能需要KVC的帮助。
#import <Foundation/Foundation.h>
@interface Object : NSObject
@property (nonatomic, strong, readonly) NSString *name;
@property (nonatomic, strong, readonly) NSString *objectId;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
@interface Object ()
@property (nonatomic, strong) NSDictionary *dictionary;
@end
@implementation Object
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
if (self = [super init]) {
self.dictionary = dictionary;
}
return self;
}
- (NSString *)name
{
return self.dictionary[@"name"];
}
- (NSString *)objectId
{
return self.dictionary[@"objectId"];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Object *object0 = [[Object alloc] initWithDictionary:@{@"name": @"victor", @"objectId": @"001"}];
Object *object1 = [[Object alloc] initWithDictionary:@{@"name": @"evan", @"objectId": @"002"}];
Object *object2 = [[Object alloc] initWithDictionary:@{@"name": @"gerry"}];
Object *object3 = [[Object alloc] initWithDictionary:@{@"name": @"tony", @"objectId": @"004"}];
NSArray *array = @[object0, object1, object2, object3];
NSArray *objectIds = [array valueForKey:@"objectId"];
NSLog(@"%@", objectIds);
NSArray *names = [array valueForKey:@"name"];
NSLog(@"%@", names);
NSArray *filteredObjectIds = [objectIds filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != nil"]];
NSLog(@"%@", filteredObjectIds);
NSMutableArray *mutableArray = [filteredObjectIds mutableCopy];
NSLog(@"%@", mutableArray);
}
return 0;
}
控制台日志:
2017-11-03 16:33:29.092837+0800 Test[50097:3891576] (
001,
002,
"<null>",
004
)
2017-11-03 16:33:29.093079+0800 Test[50097:3891576] (
victor,
evan,
gerry,
tony
)
2017-11-03 16:33:29.093317+0800 Test[50097:3891576] (
001,
002,
004
)
2017-11-03 16:33:29.093401+0800 Test[50097:3891576] (
001,
002,
004
)
Program ended with exit code: 0