Objective-c,如何从另一个类访问实例变量

Ale*_*erø 7 objective-c class-reference

我习惯用Java编程并使用类变量来访问其他类的数据.然后我发现类变量在Obj-C中的工作方式不同,并且遇到了问题.

我的问题是我想在用户登录后访问另一个类中的用户输入密码.已经阅读了不同的论坛,我应该使用类方法(+)来访问这些数据.但是因为我需要在第二类中创建第一个类的新实例,这意味着输入的密码不存在于第一类的新实例中.

我的代码如下:

class1.h

@interface class1 : UIViewController {
    UITextField *usernameField;
    UITextField *passwordField;
        UIButton *loginButton;
        NSString *password;
}

@property (nonatomic, retain) IBOutlet UITextField *usernameField;
@property (nonatomic, retain) IBOutlet UITextField *passwordField;
@property (nonatomic, retain) IBOutlet UIButton *loginButton;
@property (nonatomic, retain) NSString *password;

-(IBAction) loginButtonPushed;
+(NSString *)password;

@end
Run Code Online (Sandbox Code Playgroud)

class1.m

#import "viewSwitcherViewController.h"

@implementation viewSwitcherViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize password; // not needed

-(IBAction) loginButtonPushed {
        password = passwordField.text; //not needed
    // ...Code for switching view if successful login... EDITED:
    class2 *c2 = [class2 alloc] init]; //Instantiating new class2 object
    c2.password = passwordField.text; //Assigning tekst from passworField to thePassword-variable in the class2 instance.
}
Run Code Online (Sandbox Code Playgroud)

class2.m

#import "class2.h"
#import "class1.h"

@implementation class2

@synthesize thePassword; //NSString variable

// this method is also not needed
-(void) someMethodUsingPassword {
         class1 *c1 = [[class1 alloc] init];
         thePassword = [c1 password];
         NSLog(@"The password is: %@", thePassword); //This call returns null
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是在class2中创建的c1实例不保存提交的密码,因此返回"null".

也许这只是我的Java方法弄乱它,但我找不到任何其他方式所以请帮助:)!

Dar*_*ren 14

Java中的公共类变量等同于C和Objective-C中的全局变量.您可以将其实现为:

class1.h

extern NSString* MyGlobalPassword;
Run Code Online (Sandbox Code Playgroud)

class1.m

NSString* MyGlobalPassword = nil;
Run Code Online (Sandbox Code Playgroud)

然后,任何导入class1.h的人都可以读写MyGlobalPassword.

稍微好一点的方法是通过"类方法"使其可访问.

class1.h:

@interface Class1 : UIViewController {
    UITextField *usernameField;
    UITextField *passwordField;
    UIButton *loginButton;    
}
+ (NSString*)password;
@end
Run Code Online (Sandbox Code Playgroud)

class1.m:

static NSString* myStaticPassword = nil;

@implementation Class1
+ (NSString*)password {
    return myStaticPassword;
}

- (void)didClickLoginButton:(id)sender {
    [myStaticPassword release];
    myStaticPassword = [passwordField.text retain];
}
Run Code Online (Sandbox Code Playgroud)

然后其他类将通过password类方法读取密码.

Class2.m:

-(void) someMethodUsingPassword {
     thePassword = [Class1 password]; // This is how to call a Class Method in Objective C
     NSLog(@"The password is: %@", thePassword); 
}
Run Code Online (Sandbox Code Playgroud)


kub*_*ubi 5

您应该重新考虑应用程序结构的设置方式.您的基本需求是您需要将密码保存在一个类中,然后在另一个类中访问它,对吗?NSUserDefaults这是完美的(几乎完美,见下面的注释#3).将class1代码更改为如下所示:

-(IBAction) loginButtonPushed {
    NSString *username = self.usernameField.text;
    NSString *password = self.passwordField.text;

    // Do your login stuff here, if successful do the following
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefauls];
    [defaults setObject:username forKey:@"usernameKey"];
    [defaults setObject:password forKey:@"passwordKey"];
}
Run Code Online (Sandbox Code Playgroud)

摆脱你的密码属性,你现在不需要它.另外,摆脱你的+(NSString *)password; 方法.

当您需要访问登录名和密码时:

-(void) someMethodUsingPassword {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefauls];
    NSString *username = [defaults stringForKey:@"usernameKey"];
    NSString *password = [defaults stringForKey:@"passwordKey"];

    // do whatever here
}
Run Code Online (Sandbox Code Playgroud)

几点说明

  1. 我是从记忆中写的,如果你发现任何让我知道的话,他们可能会有错误,我会更新我的答案.
  2. 您似乎缺少关于如何使用类方法和属性的几个关键点.我非常推荐阅读Apple的Objective-C简介.
  3. 存储登录名和密码信息NSUserDefaults并不安全.如果有人获得对您的设备或设备备份的root访问权限,他们可能能够提取此登录信息.如果安全性对您至关重要,请将登录信息存储在Keychain中.