NSMutableArray 只存储一个值。使用 Parse 查询数据
NSMutableArray is only storing one value. Using Parse to query for data
我的代码查询解析并尝试将从解析中获得的值存储到数组中。然后,数组显示在新视图控制器的 table 视图中。
当我执行我的代码时,它应该会在每个数组中存储两个值。然后该数组将填充 table 视图。当我执行我的代码时,只有第一个用户的信息被存储到每个数组中。谁能在我的代码中发现导致每个数组中只存储第一个值的问题。
-(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]);
}
}}];
}
具体来说,这是我的代码部分,它将从解析中查询到的值存储到数组中。
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}"
)
我的代码未能正确执行的原因是因为我从中查询的解析数据库中的值之一是空值。这导致豁免被抛出。我现在使用的更新代码允许我解决 null 参数,如下所示。
-(IBAction)displayfriendinfo:(id)sender{
phoneNUMBERS = [NSMutableArray new];
firstNAMES =[NSMutableArray new];
lastNAMES = [NSMutableArray new];
EMAILS = [NSMutableArray new];
combinedNAMES = [NSMutableArray new];
// 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(@"%@", objects);
NSLog(@"%@", objects.class);
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) {
NSLog(@"yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs");
for (PFObject *object in objects) {
// NSLog(@"%@", objects);
// NSLog(@"%@", object.class);
NSDictionary *pn = object[@"phoneNumber"];
NSDictionary *fn = object[@"firstName"];
NSDictionary *ln= object [@"lastName"];
NSDictionary *em = object [@"email"];
NSDictionary *cm = object[@"combinedName"];
NSLog(@"phonenumber %@", pn);
NSLog(@"combined name%@", cm);
if (pn != NULL){
[phoneNUMBERS addObject:pn];
NSLog(@"number: %@",phoneNUMBERS);
}
if (fn != NULL) {
[firstNAMES addObject:fn];
NSLog(@"firstnaem: %@",firstNAMES);
}
if (ln !=NULL){
[lastNAMES addObject:ln];
NSLog(@"lastname: %@", lastNAMES);
}
if (em!=NULL) {
[EMAILS addObject:em];
NSLog(@"Email: %@", EMAILS);
}
if (cm != NULL) {
[combinedNAMES addObject:cm];
NSLog(@"combined names: %@",combinedNAMES);
}
}
}
else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}}];
我的代码查询解析并尝试将从解析中获得的值存储到数组中。然后,数组显示在新视图控制器的 table 视图中。 当我执行我的代码时,它应该会在每个数组中存储两个值。然后该数组将填充 table 视图。当我执行我的代码时,只有第一个用户的信息被存储到每个数组中。谁能在我的代码中发现导致每个数组中只存储第一个值的问题。
-(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]);
}
}}];
}
具体来说,这是我的代码部分,它将从解析中查询到的值存储到数组中。
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}"
)
我的代码未能正确执行的原因是因为我从中查询的解析数据库中的值之一是空值。这导致豁免被抛出。我现在使用的更新代码允许我解决 null 参数,如下所示。
-(IBAction)displayfriendinfo:(id)sender{
phoneNUMBERS = [NSMutableArray new];
firstNAMES =[NSMutableArray new];
lastNAMES = [NSMutableArray new];
EMAILS = [NSMutableArray new];
combinedNAMES = [NSMutableArray new];
// 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(@"%@", objects);
NSLog(@"%@", objects.class);
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) {
NSLog(@"yajdfk;adjskfas;dlfkja;klsdfjkla;sdjfk;ladfs");
for (PFObject *object in objects) {
// NSLog(@"%@", objects);
// NSLog(@"%@", object.class);
NSDictionary *pn = object[@"phoneNumber"];
NSDictionary *fn = object[@"firstName"];
NSDictionary *ln= object [@"lastName"];
NSDictionary *em = object [@"email"];
NSDictionary *cm = object[@"combinedName"];
NSLog(@"phonenumber %@", pn);
NSLog(@"combined name%@", cm);
if (pn != NULL){
[phoneNUMBERS addObject:pn];
NSLog(@"number: %@",phoneNUMBERS);
}
if (fn != NULL) {
[firstNAMES addObject:fn];
NSLog(@"firstnaem: %@",firstNAMES);
}
if (ln !=NULL){
[lastNAMES addObject:ln];
NSLog(@"lastname: %@", lastNAMES);
}
if (em!=NULL) {
[EMAILS addObject:em];
NSLog(@"Email: %@", EMAILS);
}
if (cm != NULL) {
[combinedNAMES addObject:cm];
NSLog(@"combined names: %@",combinedNAMES);
}
}
}
else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}}];