将 PFQuery 解析为数组 objective-c
Parse PFQuery to array objective-c
所有功能都基于 Parse。我有一个名为 "Connect" 的 class,其中包含用户帐户。我的代码获取记录并将它们显示在 NSLog 中。但我需要他带领他们进入 NSArray。我的 NSArray 名为 userPost 和 userName。
PFQuery *query = [PFQuery queryWithClassName:@"Connect"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %lu messages.", (unsigned long)objects.count);
for (PFObject *object in objects) {
NSLog(@"object with id has location %@ %@", object.objectId, artistName);
NSLog(@"object with id has location %@ %@", object.objectId, artistMessage);
}
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
我的第一个解决方案的唯一问题是您需要设置一个 NSObject subclass 模型来描述您在 Parse.com 上的 class 并加载每个字典从响应数组到您查询的每个字典的新对象。您可以下拉整个数组,但您知道,如果您在 Parse.com 上有一个与其他 table 互连的 class,则该数组包含大量嵌套信息通过指针。
实际上,这是您需要的代码,我将 post 整个代码集:
在头文件中,像这样声明一个 属性:
@property (nonatomic, strong)NSArray *ninjas;
- (void)loadNinjas
{
PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
[query includeKey:@"moreStuff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
if (!error) {
NSMutableArray *tempNinjas = [[NSMutableArray alloc] init];
for (PFObject *dic in a) {
NSHStuff *ninja = [[NSHStuff alloc] initWithDictionary:dic];
[tempNinjas addObject:ninja];
}
self.ninjas = [[NSArray alloc] initWithArray:tempNinjas];
tempNinjas = nil;
dispatch_async(dispatch_get_main_queue(), ^ {
[self.contentView.tableView reloadData];
});
}
}];
}
只需获取对象数组:
- (void)loadNinjas
{
PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
[query includeKey:@"moreStuff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
if (!error) {
self.ninjas = a;
}
}];
}
这是返回数组的内部结构:
stuff: 0x7fcf15avv3af1e0, objectId: L4adsfeafeDHSky, localId: (null)> {\n ACL = \"\";\n variable1 = false;\n moreStuff = \"\";\n counter1 = 0; \n justatextbodyfortesting = \"Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test \";\n \"anotherconter\" = 0;\n title = \"This is the title\";\n userId = \"\";\n}",
上面的"stuff"表示Parse中一个table的1行所有的指针信息都连接起来
对于 table 中的每一行,您将有很多次像这样的 "stuff",这意味着从 Parse 返回的数组有大量的字典,每个字典包含您正在查询的 table 的整行。
Parse 对用户帐户有特殊的 User
class。您的问题尚不清楚,但是如果您想将接收到的对象存储在 NSArray
中,您可以创建当前数组的 NSMutableArray
副本,使用 [array addObject:object]
方法添加对象并将其值分配给原始数组。
所有功能都基于 Parse。我有一个名为 "Connect" 的 class,其中包含用户帐户。我的代码获取记录并将它们显示在 NSLog 中。但我需要他带领他们进入 NSArray。我的 NSArray 名为 userPost 和 userName。
PFQuery *query = [PFQuery queryWithClassName:@"Connect"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Successfully retrieved %lu messages.", (unsigned long)objects.count);
for (PFObject *object in objects) {
NSLog(@"object with id has location %@ %@", object.objectId, artistName);
NSLog(@"object with id has location %@ %@", object.objectId, artistMessage);
}
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
我的第一个解决方案的唯一问题是您需要设置一个 NSObject subclass 模型来描述您在 Parse.com 上的 class 并加载每个字典从响应数组到您查询的每个字典的新对象。您可以下拉整个数组,但您知道,如果您在 Parse.com 上有一个与其他 table 互连的 class,则该数组包含大量嵌套信息通过指针。
实际上,这是您需要的代码,我将 post 整个代码集: 在头文件中,像这样声明一个 属性:
@property (nonatomic, strong)NSArray *ninjas;
- (void)loadNinjas
{
PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
[query includeKey:@"moreStuff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
if (!error) {
NSMutableArray *tempNinjas = [[NSMutableArray alloc] init];
for (PFObject *dic in a) {
NSHStuff *ninja = [[NSHStuff alloc] initWithDictionary:dic];
[tempNinjas addObject:ninja];
}
self.ninjas = [[NSArray alloc] initWithArray:tempNinjas];
tempNinjas = nil;
dispatch_async(dispatch_get_main_queue(), ^ {
[self.contentView.tableView reloadData];
});
}
}];
}
只需获取对象数组:
- (void)loadNinjas
{
PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
[query includeKey:@"moreStuff"];
[query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
if (!error) {
self.ninjas = a;
}
}];
}
这是返回数组的内部结构:
stuff: 0x7fcf15avv3af1e0, objectId: L4adsfeafeDHSky, localId: (null)> {\n ACL = \"\";\n variable1 = false;\n moreStuff = \"\";\n counter1 = 0; \n justatextbodyfortesting = \"Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test \";\n \"anotherconter\" = 0;\n title = \"This is the title\";\n userId = \"\";\n}",
上面的"stuff"表示Parse中一个table的1行所有的指针信息都连接起来
对于 table 中的每一行,您将有很多次像这样的 "stuff",这意味着从 Parse 返回的数组有大量的字典,每个字典包含您正在查询的 table 的整行。
Parse 对用户帐户有特殊的 User
class。您的问题尚不清楚,但是如果您想将接收到的对象存储在 NSArray
中,您可以创建当前数组的 NSMutableArray
副本,使用 [array addObject:object]
方法添加对象并将其值分配给原始数组。