I have used the AFJSONRequest item in my app and the data it returns in the success portion I want to use in other parts of the view controller. I can’t for the life of me figure it out. I have created a property to hold the data but I can’t access it in other methods. Any reason for this? It’s probably something I’m doing wrong. How do I use the nameArray, emailArray, and passArray in other methods? They are already declared properties. Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://10.247.245.87/it/emailmanager.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"email.last_name is of type: %@", [[JSON valueForKeyPath:@"email.lastname" ] class]);
nameArray = [JSON valueForKeyPath:@"email.last_name"];
emailArray = [JSON valueForKeyPath:@"email.email_address"];
passArray = [JSON valueForKeyPath:@"email.password"];
NSLog(@"%@", nameArray);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"error %@ %@", [error description], JSON);
}];
[operation start];
}
And here is the rest.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
EMailCell* cell = [tableView dequeueReusableCellWithIdentifier:@"EmailCell"];
cell.nameLabel.text = @"%@", nameArray;
cell.emailLabel.text = @"%@", emailArray;
cell.passLabel.text = @"%@", passArray;
return cell;
}
Here is the structure of the JSON results:
2012-11-13 07:43:46.026 IT Tools[13987:c07] Names: (
{
code = 0496;
department = Management;
"email_address" = "????@???.com";
"first_name" = John;
id = 227;
"last_name" = Doe;
password = "T0496)$(^";
store = Toyota;
},
And the result from the debugger console:
2012-11-13 09:04:37.416 IT Tools[15386:c07] email.last_name is of type: __NSArrayI
You are assigning arrays to string values. You should loop through the JSON results and create arrays of strings… It would help to know the structure of your JSON results. If you are using core data, there is a handy method for parsing JSON into core data objects.