I'm somewhat of a newbie to objective-c programming, and can't understand how to create a NSObject in one method and use it in another.
For example:
I have a UserObject with properties like firstName, lastName.
@interface UserObject : NSObject
@property (nonatomic, retain) NSString *userID, *firstName, *lastName, *profilePic, *fullName, *email, *twitter, *followingCount, *followerCount;
@end
In my profileViewController.h I declare currentUser as @property (retain, nonatomic) UserObject *currentUser;
Now, here's the problem. I have this IBAction
- (IBAction)followUser:(id)sender {
NSLog(currentUser.firstName);
}
After receiving json data from a server, I run a method called ConnectionDidFinishLoading and inside ->
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dataArray = [json JSONValue];
UserObject *currentUserData = [[UserObject alloc] init];
currentUserData.firstName = [dataArray objectForKey:@"first_name"];
currentUserData.lastName = [dataArray objectForKey:@"last_name"];
currentUser = currentUserData;
[dataArray release];
[json release];
[currentUserData release];
}
Now, here's the problem. When I run this IBAction, the app crashes.
- (IBAction)followUser:(id)sender {
NSLog(@"%@",currentUser.firstName);
}
I'm pretty sure it's because the currentUser is not available to this method. Is there a way to make the currentUser object global so I can grab it in any method?