Objective-c, how to access an instance variable from another class

后端 未结 4 1679
耶瑟儿~
耶瑟儿~ 2020-12-28 23:13

I am used to programming in Java and to use class variables to access data from other classes. Then I found out that class variables does not work the same way in Obj-C, and

相关标签:
4条回答
  • 2020-12-28 23:54

    You should rethink how your app structure is set up. Your basic need is that you need to save your password in one class and access it in another, right? NSUserDefaults is perfect (almost perfect, see note #3 below) for this. Change your class1 code to look like this:

    -(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"];
    }
    

    Get rid of your password property, you don't need it for anything now. Also, get rid of your +(NSString *)password; class method.

    When you need to access the login and password:

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

    A few notes

    1. I wrote this from memory, so they may have errors if you spot any let me know and I'll update my answer.
    2. You seem to be missing a few key points about how to use class methods and properties. I highly recomend reading Apple's Introduction to Objective-C.
    3. Storing login and password information in NSUserDefaults is NOT secure. If someone gains root access to your device or to your device backups, they may be able to extract this login information. If security is crucial to you, store the login information in Keychain.
    0 讨论(0)
  • 2020-12-29 00:01

    A public class variable in Java is equivalent to a global variable in C and Objective-C. You would implement it as:

    class1.h

    extern NSString* MyGlobalPassword;
    

    class1.m

    NSString* MyGlobalPassword = nil;
    

    Then, anyone that imports class1.h can read and write to MyGlobalPassword.

    A slightly better approach would be make it accessible via a "class method".

    class1.h:

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

    class1.m:

    static NSString* myStaticPassword = nil;
    
    @implementation Class1
    + (NSString*)password {
        return myStaticPassword;
    }
    
    - (void)didClickLoginButton:(id)sender {
        [myStaticPassword release];
        myStaticPassword = [passwordField.text retain];
    }
    

    Other classes would then read the password through the password class method.

    Class2.m :

    -(void) someMethodUsingPassword {
         thePassword = [Class1 password]; // This is how to call a Class Method in Objective C
         NSLog(@"The password is: %@", thePassword); 
    }
    
    0 讨论(0)
  • 2020-12-29 00:08

    The class1 created in class2 won't have a password set, because you just instantiated a new instance with [[class1 alloc] init] -- this will not share member variables with other instances of class1 created elsewhere.

    0 讨论(0)
  • 2020-12-29 00:10

    Generally,

    When you accept the password in one screen and do whatever with it, you still have it. When you are moving to someother screen why dont you keep, class1's object as a property in class2 and set it before u pass the control to class 2.

    Or if you are just calling the method of class2 ust pass class1 obj as a parameter with password already in it.

    and in case u just pass it to the method as a parameter, u can access it. Or if you are setting it as a property of this class2. then u can always use a getter.

    This thing has got nothing t do with Java or Objective C.

    0 讨论(0)
提交回复
热议问题