简体   繁体   中英

Getting NSCFString objectAtIndexedSubscript with NSLog in cellForRowAtIndexPath

I've finished my app using this method and the last call I'm implementing is a simple JSON response which I need to parse and it is giving me:

[__NSCFString objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x17005caa0'

There are a lot of smililar questions like this on this website but I'm doing everything as they directed but still no luck.
It's a nested JSON in responseObject. I'm getting the responseObject which means the request I'm sending is correct.

[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
        if (!error) {
            if (response) {
                allResponse = [[NSMutableArray alloc] init];
                allPractice = [[NSMutableArray alloc] init];
                allProvider = [[NSMutableArray alloc] init];

                allFirstName = [[NSMutableArray alloc] init];
                allMiddleName = [[NSMutableArray alloc] init];
                allLastName = [[NSMutableArray alloc] init];
                allPracticeName = [[NSMutableArray alloc] init];

                allResponse = responseObject;
                allPractice = [allResponse valueForKey:@"Practice"];
                allProvider = [allResponse valueForKey:@"Provider"];

                allFirstName = [allProvider valueForKey:@"FirstName"];
                allMiddleName = [allProvider valueForKey:@"MiddleName"];
                allLastName = [allProvider valueForKey:@"LastName"];
                allPracticeName = [allPractice valueForKey:@"PracticeAlias"];

                [self.tableView reloadData];
                [SVProgressHUD dismiss];

            }}

Here is the structure I'm getting as ResponseObjects.

{
    AccountType = 2;
    CreatedBy = smith;
    CreatedDate = "2016-01-25T02:07:42.45";
    DisplayName = ",  ";
    Email = "<null>";
    ErrorMessage = "<null>";
    FirstName = "<null>";
    ID = 1;
    IsActive = 1;
    Password = test;
    Phone = "<null>";
    Practice =     {
PracticeAlias = “SAMPLE MEDICAL GROUP";
};
    PracticeAlias = "<null>";
    PracticeCode = "<null>";
    PracticeID = 1;
Provider =     {
    AccountType = 0;
    CreatedBy = smith;
    LastName = John;
    MiddleName = "";
    FirstName = Smith;
};
    Staff = "<null>";
    lstPractice = "<null>";
    lstProvider = "<null>";
    pageSize = 0;
}

So in cellForRowAtIndexPath when I do this:

NSLog(@"%@", [NSString stringWithFormat:@"%@",allPracticeName[indexPath.row]]);

The app crash with the message I shared above.

Look at the two lines

            allResponse = [[NSMutableArray alloc] init];
            allResponse = responseObject;

That's nonsense. You set allResponse to a newly created mutable array, and then you replace it with responseObject. And there is no checking whatsoever. Any unexpected data will make your app crash.

You should make responseObject an NSDictionary* - and then check that it is indeed an NSDictionary*. Do NOT use valueForKey unless you can give a very good reason why you do that instead of just using responseObject [@"Practice"] for example. And check those types as well.

allPracticeName is of type NSString (with value "< null >"). You are calling a subscript operator [] on this string:

allPracticeName[indexPath.row]

but NSString has no subscript implementation.

Remove the subscript:

NSLog(allPracticeName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM