Brittany
Brittany

Reputation: 1449

Obj-C: NSString crashes with error "Terminating app due to uncaught exception NSInvalidArgumentException"?

For some reason, the following line in my app is returned NULL, and thus, crashes my app:

NSString *address = [session user][@"field_street_address"][@"und"][0][@"safe_value"];

Which, I don't understand, as my console below states that data is returned for field_street_address. Is there something wrong with that line that I'm just not seeing? I've been staring at this for a while and I feel like I'm missing something obvious.

ViewController.m

NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];

DIOSSession *session = [DIOSSession sharedSession];

[session setUser:userDictInfo];

[session user];

NSString *address = [session user][@"field_street_address"][@"und"][0][@"safe_value"];

Console ([session user] log):

2017-10-06 14:06:22.226970-0700 app[828:193706] {
    sessid = "DRY0fOXtO_FZOIeowFVVq8oalaFnKSe";
    "session_name" = SESS2bb8896be0f16543ff3c6a;
    token = giCdHBuw967IaSxDB34m0Evzf1HI3DIK6;
    user =     {
        access = 1507310936;
        created = 1459875505;
        data =         {
            "ckeditor_auto_lang" = t;
            "ckeditor_default" = t;
            "ckeditor_lang" = en;
            "ckeditor_show_toggle" = t;
            "ckeditor_width" = "100%";
        };
        "field_address" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "1325 Fake Street";
                    value = "1325 Fake Street";
                }
            );
        };
        "field_childrenunder" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = No;
                    value = No;
                }
            );
        };
        "field_city" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = Van;
                    value = Van;
                }
            );
        };
        "field_emergency_facility" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = Yes;
                    value = Yes;
                }
            );
        };
        "field_first_name" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = Brittany;
                    value = Brittany;
                }
            );
        };
        "field_last_name" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = B;
                    value = B;
                }
            );
        };
        "field_phonenumber" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = 2369893091;
                    value = 2369893091;
                }
            );
        };
        "field_photo_path" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "sites/default/files/stored/1507092784.jpg";
                    value = "sites/default/files/stored/1507092784.jpg";
                }
            );
        };
        "field_points_balance" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = 12;
                    value = 12;
                }
            );
        };
        "field_postal_code" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = 000000;
                    value = 000000;
                }
            );
        };
        "field_private_message_notify" =         {
            und =             (
                                {
                    value = 1;
                }
            );
        };
        "field_profile_photo" =         {
            und =             (
                                {
                    alt = "";
                    fid = 237;
                    "field_file_image_alt_text" =                     (
                    );
                    "field_file_image_title_text" =                     (
                    );
                    filemime = "image/jpeg";
                    filename = "1507092784.jpg";
                    filesize = 16084;
                    height = 296;
                    metadata =                     {
                        height = 296;
                        width = 300;
                    };
                    "rdf_mapping" =                     (
                    );
                    status = 1;
                    timestamp = 1507108254;
                    title = "";
                    type = image;
                    uid = 47;
                    uri = "public://stored/1507092784.jpg";
                    width = 300;
                }
            );
        };
        "field_property_type" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = House;
                    value = House;
                }
            );
        };
        "field_province" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = BC;
                    value = BC;
                }
            );
        };
        "field_special_skills" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "Oral medication";
                    value = "Oral medication";
                }
            );
        };
        "field_star_rating" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = 1;
                    value = 1;
                }
            );
        };
        "field_street_address" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "1325 Fake Street";
                    value = "1325 Fake Street";
                }
            );
        };
        "field_supervision" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = No;
                    value = No;
                }
            );
        };
        "field_userbio" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "Hi my name is Brittany.";
                    value = "Hi my name is Brittany.";
                }
            );
        };
        language = "";
        login = 1507320712;
        mail = "[email protected]";
        name = Brittany;
        picture = "<null>";
        "rdf_mapping" =         {
            homepage =             {
                predicates =                 (
                    "foaf:page"
                );
                type = rel;
            };
            name =             {
                predicates =                 (
                    "foaf:name"
                );
            };
            rdftype =             (
                "sioc:UserAccount"
            );
        };
        roles =         {
            2 = "authenticated user";
        };
        signature = "";
        "signature_format" = "filtered_html";
        status = 1;
        theme = "";
        timezone = UTC;
        uid = 47;
    };
}

Upvotes: 0

Views: 67

Answers (1)

rmaddy
rmaddy

Reputation: 318944

If the log output in your question is from logging [session user] then you need to first access the @"user" key.

NSString *address = [session user][@"user"][@"field_street_address"][@"und"][0][@"safe_value"];

BTW - for issues like this it really helps to break down the code:

NSDictionary *sessionUser = [session user];
NSDictionary *user = sessionUser[@"user"];
NSDicitonary *streetAddr = user[@"field_street_address"];
// etc.

Then you can see where you start getting nil and look at the previous results to determine where things are going wrong.

Upvotes: 3

Related Questions