我对 Objective-c 编程有点陌生,无法理解如何在一种方法中创建 NSObject 并在另一种方法中使用它。
例如:
我有一个具有名字、姓氏等属性的用户对象。
@interface UserObject : NSObject
@property (nonatomic, retain) NSString *userID, *firstName, *lastName, *profilePic, *fullName, *email, *twitter, *followingCount, *followerCount;
@end
在我的 profileViewController.h 中,我将 currentUser 声明为@property (retain, nonatomic) UserObject *currentUser;
现在,问题来了。我有这个 IBAction
- (IBAction)followUser:(id)sender {
NSLog(currentUser.firstName);
}
从服务器接收到 json 数据后,我运行了一个名为 ConnectionDidFinishLoading 的方法并在里面 ->
- (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];
}
现在,问题来了。当我运行这个 IBAction 时,应用程序崩溃了。
- (IBAction)followUser:(id)sender {
NSLog(@"%@",currentUser.firstName);
}
我很确定这是因为 currentUser 不适用于此方法。有没有办法让 currentUser 对象成为全局对象,这样我就可以用任何方法来获取它?