【问题标题】:NSMutableArray is only storing one value. Using Parse to query for dataNSMutableArray 只存储一个值。使用 Parse 查询数据
【发布时间】: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


【解决方案1】:

我的代码未能正确执行的原因是我从中查询的解析数据库中的值之一是空值。这导致豁免被抛出。我现在使用的允许我解决 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]);
        }
    }}];

【讨论】:

    猜你喜欢
    • 2018-09-19
    • 1970-01-01
    • 2013-07-17
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    相关资源
    最近更新 更多