你如何在所有方法中使NSObject“全局”/可用?

时间:2022-09-25 07:41:47

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.

我有点像Objective-c编程的新手,并且无法理解如何在一个方法中创建NSObject并在另一个方法中使用它。

For example:

I have a UserObject with properties like firstName, lastName.

我有一个UserObject,其属性如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;

在我的profileViewController.h中,我将currentUser声明为@property(retain,nonatomic)UserObject * currentUser;

Now, here's the problem. I have this IBAction

现在,这是问题所在。我有这个IBAction

- (IBAction)followUser:(id)sender {
    NSLog(currentUser.firstName);
}

After receiving json data from a server, I run a method called ConnectionDidFinishLoading and inside ->

从服务器接收到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];
}

Now, here's the problem. When I run this IBAction, the app crashes.

现在,这是问题所在。当我运行这个IBAction时,应用程序崩溃了。

- (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?

我很确定这是因为currentUser不适用于这种方法。有没有办法使currentUser对象全局,所以我可以在任何方法中获取它?

3 个解决方案

#1


4  

I think you're confused about the different between instance variables and properties. You're setting the currentUser instance variable directly, which does not retain the object — so assuming you're not using ARC, it'll get destroyed too early. You need to change the line where currentUser is set to one of these:

我认为你对实例变量和属性之间的差异感到困惑。你直接设置currentUser实例变量,它不保留对象 - 所以假设你没有使用ARC,它会被过早销毁。您需要将currentUser设置为其中一行的行更改为:

currentUser = [currentUserData retain];
// OR
self.currentUser = currentUserData;

The self.currentUser syntax is how you access the property. Without the dot, you're accessing the ivar directly.

self.currentUser语法是您访问该属性的方式。如果没有圆点,您将直接访问ivar。

#2


1  

Try this :

尝试这个 :

NSLog(@"%@",currentUser.firstName);

Hint : %s is used for C-style strings.

提示:%s用于C风格的字符串。

#3


1  

The problem is most likely that you are calling the followUser: method before any data is received from your server, so currentUser hasn't been created, so it is a null/dangling pointer, which is most likely crashing your app. Do a test to make sure currentUser isn't nil before using it:

问题很可能是在从服务器收到任何数据之前调用followUser:方法,因此尚未创建currentUser,因此它是一个空/悬空指针,很可能会使您的应用程序崩溃。在使用之前进行测试以确保currentUser不为零:

if(currentUser) {
    //do what you want
    //if currentUser is nil, this if statement will evaluate to false
    NSLog(@"%@", currentUser.firstName);
}

#1


4  

I think you're confused about the different between instance variables and properties. You're setting the currentUser instance variable directly, which does not retain the object — so assuming you're not using ARC, it'll get destroyed too early. You need to change the line where currentUser is set to one of these:

我认为你对实例变量和属性之间的差异感到困惑。你直接设置currentUser实例变量,它不保留对象 - 所以假设你没有使用ARC,它会被过早销毁。您需要将currentUser设置为其中一行的行更改为:

currentUser = [currentUserData retain];
// OR
self.currentUser = currentUserData;

The self.currentUser syntax is how you access the property. Without the dot, you're accessing the ivar directly.

self.currentUser语法是您访问该属性的方式。如果没有圆点,您将直接访问ivar。

#2


1  

Try this :

尝试这个 :

NSLog(@"%@",currentUser.firstName);

Hint : %s is used for C-style strings.

提示:%s用于C风格的字符串。

#3


1  

The problem is most likely that you are calling the followUser: method before any data is received from your server, so currentUser hasn't been created, so it is a null/dangling pointer, which is most likely crashing your app. Do a test to make sure currentUser isn't nil before using it:

问题很可能是在从服务器收到任何数据之前调用followUser:方法,因此尚未创建currentUser,因此它是一个空/悬空指针,很可能会使您的应用程序崩溃。在使用之前进行测试以确保currentUser不为零:

if(currentUser) {
    //do what you want
    //if currentUser is nil, this if statement will evaluate to false
    NSLog(@"%@", currentUser.firstName);
}