【发布时间】:2015-03-08 01:28:16
【问题描述】:
我的代码查询解析并尝试将从解析获得的值存储到数组中。然后数组显示在新视图控制器的表视图中。 当我执行我的代码时,它应该导致在每个数组中存储两个值。然后这个数组将填充一个表格视图。当我执行我的代码时,只有第一个用户的信息存储到每个数组中。任何人都可以在我的代码中发现导致每个数组中仅存储第一个值的问题。
-(IBAction)displayfriendinfo:(id)sender{
phoneNUMBERS = [NSMutableArray new];
firstNAMES =[NSMutableArray new];
lastNAMES = [NSMutableArray new];
EMAILS = [NSMutableArray new];
combinedNAMES = [[NSMutableArray alloc] init];
// Initialize table data
PFQuery *query = [PFQuery queryWithClassName:@"friendsAssociation"];
//quesrys the class Friend asssociation to find when instances of "user" equal the
//logged in user's username
[query whereKey:@"user" equalTo:usernamecontrol];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"Successfully retrieved %d users.", objects.count);
// Do something with the found objects
ContactTableViewController *contacts = [[ContactTableViewController alloc]initWithNibName:Nil bundle:Nil];
contacts.user = usernamecontrol;
contacts.COMBINEDNAMES = [NSMutableArray new];
contacts.FIRSTNAMES = [NSMutableArray new];
contacts.PHONENUMBERS = [NSMutableArray new];
contacts.LASTNAMES = [NSMutableArray new];
contacts.COMBINEDNAMES = combinedNAMES;
contacts.PHONENUMBERS = phoneNUMBERS;
contacts.FIRSTNAMES = firstNAMES;
contacts.LASTNAMES = lastNAMES;
[self presentViewController:contacts animated:YES completion:Nil];
if (objects.count == 0) {
//uialert letting the user know that no phone number matches the query
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Friends"
message:@"No Friends"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
//if there is only one number matching the query
if (objects.count >=1) {
for (PFObject *object in objects) {
NSLog(@"%@", objects);
NSString *phonenumber = object[@"phoneNumber"];
NSString *firstname = object[@"firstName"];
NSString *lastname = object [@"lastName"];
NSString *email = object [@"email"];
NSString *combinedname = object[@"combinedName"];
NSLog(@"%@", phonenumber);
NSLog(@"%@", combinedname);
[phoneNUMBERS addObject:phonenumber];
[firstNAMES addObject:firstname];
[lastNAMES addObject:lastname];
[EMAILS addObject:email];
[combinedNAMES addObject:combinedname];
NSLog(@"yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs");
NSLog(@"%@",combinedNAMES);
NSLog(@"%@",phoneNUMBERS);
}
//if there is more than one phonenumber matching the query as
//the user to input the friends username
//instead
}
else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}}];
}
这里是我的代码的一部分,它将从 parse 查询的值存储到数组中。
for (PFObject *object in objects) {
NSLog(@"%@", objects);
NSString *phonenumber = object[@"phoneNumber"];
NSString *firstname = object[@"firstName"];
NSString *lastname = object [@"lastName"];
NSString *email = object [@"email"];
NSString *combinedname = object[@"combinedName"];
NSLog(@"%@", phonenumber);
NSLog(@"%@", combinedname);
[phoneNUMBERS addObject:phonenumber];
[firstNAMES addObject:firstname];
[lastNAMES addObject:lastname];
[EMAILS addObject:email];
[combinedNAMES addObject:combinedname];
NSLog(@"yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs");
NSLog(@"%@",combinedNAMES);
NSLog(@"%@",phoneNUMBERS);
}
除了 NSLog(@"yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs"); 从不显示。但是, NSLog(@"%@", objects);准确显示两个用户信息。 这是输出
2015-03-07 20:33:07.626 ContactKeeper[46067:3405123] Successfully retrieved 2 users.
2015-03-07 20:33:07.630 ContactKeeper[46067:3405123] (
"<friendsAssociation: 0x7abafd30, objectId: pcBKXGfASi, localId: (null)> {\n ACL = \"<PFACL: 0x7ab7c740>\";\n combinedName = \"lewis meyer\";\n firstName = lewis;\n friendUsername = Louis;\n lastName = meyer;\n phoneNumber = 5555228243;\n user = David;\n}",
"<friendsAssociation: 0x7aba3c70, objectId: y1ToExVLew, localId: (null)> {\n ACL = \"<PFACL: 0x7abb3020>\";\n combinedName = \"John Apple\";\n email = \"rotha@g.com\";\n firstName = John;\n friendUsername = John;\n lastName = Apple;\n phoneNumber = 7075551854;\n user = David;\n}"
)
【问题讨论】:
-
你显然错了。在上面的代码中,如果第一个 NSLog 发生并且没有错误,那么剩下的 5 个 NSLog 将会发生。你应该打印
objects.count。 -
我在 NSLog(@"Successfully retrieved %d users.", objects.count);这会打印出正确的用户数量
-
那么究竟是打印了什么?您声称“yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs”从打印出来,如果代码没有崩溃,这似乎令人难以置信。
-
因此,当您尝试将这些字符串视为 NSDictionarys 时,代码会崩溃。
-
据我所知,该代码永远不会崩溃,但是当 tableview 加载时,它会显示 NSMutable Array COMBINEDNAMES,但只显示 lewis meyer 而不会显示 John Apple。
标签: ios objective-c parse-platform nsmutablearray