I am programming an application in which I have a list of users received from server in format JSON. Later I extract from that JSON the data of each user and I have created an object called UsersController with four fields (I don't know if this is the adequate word) called prod_id, userName, userThumb and createTime. In my ViewController I have an object of UsersController and an array for store all the users. The code of the class UsersController is:
//UsersController.h
#import <UIKit/UIKit.h>
@interface UsersController : NSObject{
NSInteger *prof_id;
NSString *createTime;
NSString *fullName;
NSString *thumb;
}
@property (nonatomic) NSInteger *prof_id;
@property (nonatomic, retain) IBOutlet NSString *createTime;
@property (nonatomic, retain) IBOutlet NSString *fullName;
@property (nonatomic, retain) IBOutlet NSString *thumb;
@end
//UsersController.m
#import "UsersController.h"
@implementation UsersController
@synthesize prof_id;
@synthesize fullName;
@synthesize createTime;
@synthesize thumb;
@end
And in ViewController.m I have the following code:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSArray *array_webdata=[[NSArray array] init];
NSString *searchStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
array_webdata = [parsedata objectWithString:searchStatus error:nil];
//String with all data of each user
NSString *usersList = [array_webdata valueForKey:@"results"];
NSLog(@"\n usersList =\n %@ \n", usersList);
//extract data from the JSON data received from the server
NSArray *userName = [usersList valueForKey:@"fullname"];
NSArray *userID = [usersList valueForKey:@"prof_id"];
NSArray *userThumb = [usersList valueForKey:@"thumb"];
NSArray *userCreateTime = [usersList valueForKey:@"createTime"];
NSMutableArray *arrayUsuarios = [[NSMutableArray alloc] initWithCapacity: [userID count]];
int i;
UsersController *usuarioSimple;
UsersController *usuarioAuxiliar;
for (int i=0; i<[userName count]; i++) {
usuarioSimple = [[UsersController alloc] init];
usuarioAuxiliar= [[UsersController alloc] init];
//I store in the object called usuario the extracted data from JSON userName, userID, userThumb y userCreateTime
usuarioSimple.prof_id = [userID objectAtIndex:i];
usuarioSimple.fullName = [userName objectAtIndex:i];
usuarioSimple.thumb = [userThumb objectAtIndex:i];
usuarioSimple.createTime = [userCreateTime objectAtIndex:i];
[arrayUsuarios addObject:usuarioSimple];
usuarioAuxiliar = [arrayUsuarios objectAtIndex:i];
NSLog(@"pruebaConsulta.prof_id[%@]: %@",i, usuarioAuxiliar.prof_id);
NSLog(@"pruebaConsulta.fullName[%@]: %@",i, usuarioAuxiliar.fullName);
NSLog(@"pruebaConsulta.thumb[%@]: %@",i, usuarioAuxiliar.thumb);
NSLog(@"pruebaConsulta.createTime[%@]: %@\n",i, usuarioAuxiliar.createTime);
[usuarioSimple release];
[usuarioAuxiliar release];
}
[searchStatus release];
[connection release];
[webData release];
[pool drain];
}
And here I have two problems. The first is the declaration of i used in bucle for. I don't know why but when I execute, when it has to show the value of i, displays (null).
The second problem is when I use [userID count], [userName count], [userThumb count] or [userCreateTime count]. This instructions doesn't work because if I write this line:
NSLog(@"userid count: %@", [userID count]);
the execution crashes and says EXC_BAD_ACCESS. I proved a lot of possible solutions but always fails, please I need your help. Thanks.
ps: sorry for my english!