0

I am only starting with IOS Development and creating an IOS application (Final project) for my computer science class which uses JSON to get data from remote MySql database.

Here is the method I call from ViewDidLoad:

- (void)loadJSON
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:@"http://vito-service.ru/studentsconnect/profile.php"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

//-- JSON Parsing
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);

}

and NSLog prints the correct data:

Result = {
users =     (
            {
        IdUser = 1;
        facebook = "";
        fullname = "";
        instagram = "";
        phonenumber = "";
        username = moderas;
    },
            {
        IdUser = 2;
        facebook = "fb.com/petroffmichael";
        fullname = "Michael Perov";
        instagram = "@petroffmichael";
        phonenumber = "(650)557-6168";
        username = petroffmichael;
    },
            {
        IdUser = 3;
        facebook = "fb.com/testuser";
        fullname = "Test User";
        instagram = "@testuser";
        phonenumber = "111 111-1111";
        username = testuser;
    }
);
}

But how can I retrieve this data as Strings to put them to the labels then? I tried lots of different options of creating Data objects, which I found here on stack overflow but I always get an "unrecognized selector sent to instance" error. I also tried creating a separate NSObject

@synthesize IdUser, username, fullname, phonenumber, facebook, instagram;

- (id) initWithUsername: (NSString *)uUsername andUserFullName: (NSString *)uFullname andUserPhonenumber: (NSString *)uPhonenumber andUserFacebook: (NSString *)uFacebook andUserInstagram: (NSString *)uInstagram andUserId: (NSString *)uId
{
self = [super init];
if (self) {
    username = uUsername;
    fullname = uFullname;
    phonenumber = uPhonenumber;
    facebook = uFacebook;
    instagram = uInstagram;
    IdUser = uId;
}

return self;
}

and then retrieving data with this method:

-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

usersArray = [[NSMutableArray alloc] init];

for (int i=0; i<jsonArray.count; i++) {
    NSString * uId = [[jsonArray objectAtIndex: i] objectForKey:@"IdUser"];
    NSString * uUsername = [[jsonArray objectAtIndex: i] objectForKey:@"username"];
    NSString * uFullname = [[jsonArray objectAtIndex: i] objectForKey:@"fullname"];
    NSString * uPhonenumber = [[jsonArray objectAtIndex: i] objectForKey:@"phonenumber"];
    NSString * uFacebook = [[jsonArray objectAtIndex: i] objectForKey:@"facebook"];
    NSString * uInstagram = [[jsonArray objectAtIndex: i] objectForKey:@"instagram"];

    [usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}

}

I have all the important #import's but I still get the same error:

2014-04-23 18:35:26.612 testimage[47155:70b] -[__NSCFDictionary objectAtIndex:]:     unrecognized selector sent to instance 0xa72a8b0
2014-04-23 18:35:26.813 testimage[47155:70b] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]:     unrecognized selector sent to instance 0xa72a8b0'

Can anybody please help to find the right solution? Thanks!

After listening to multiple comments I changed the code but still receive an error..

I guess I am still doing something wrong

jsonArray is not an array anymore:

@property (nonatomic, strong) NSDictionary *jsonArray;
@property (nonatomic, strong) NSMutableArray *usersArray;

Then the method:

-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];

for (NSDictionary *dict in jsonArray)
{
    NSString * uId = [dict objectForKey:@"IdUser"];
    NSString * uUsername = [dict objectForKey:@"username"];
    NSString * uFullname = [dict objectForKey:@"fullname"];
    NSString * uPhonenumber = [dict objectForKey:@"phonenumber"];
    NSString * uFacebook = [dict objectForKey:@"facebook"];
    NSString * uInstagram = [dict objectForKey:@"instagram"];

    [usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}

}

And I am still getting the same error:

2014-04-23 20:44:43.971 testimage[48285:70b] -[__NSCFString objectForKey:]:  unrecognized selector sent to instance 0xa5547d0
2014-04-23 20:44:44.053 testimage[48285:70b] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]:   unrecognized selector sent to instance 0xa5547d0'
1
  • 2
    You left the most important piece of info out of your question - the full and exact error message and which line is causing it. Commented Apr 24, 2014 at 1:32

2 Answers 2

1

Look closely at that output. It isn't what you say. What you have there is not an array — it's a dictionary with one key, @"users", whose value is an array. That's what it's telling you when it starts with { users = …. If you want the array, you'll need to retrieve it from the dictionary.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *result = [res objectForKey:@"users"];

This will do. The response isn't a NSArray. It is a Dictionary. First get the array from the dictionary and then use it as you are using. Hope this helps... :)

EDIT:

You can rewrite the loop by this:

for (NSDictionary *dict in jsonArray)
{
    //dict is a NSDictionary. Just get value for keys
}

3 Comments

Or with modern syntax: NSMutableArray *result = res[@"users"];.
Works fine! But how to retrieve data (as strings) from this NSMutableArray? Just like from a normal array?
Your each index contains a NSDictionary. You were doing it wright: NSString * uId = [[jsonArray objectAtIndex: i] objectForKey:@"IdUser"]; or you can write this as @rmaddy suggested NSString * uId = [[jsonArray objectAtIndex: i] [@"IdUser"]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.