Question

In creating a login screen with static logins I'm trying to store them privately in the following class implementation. When a button creates IONServer objects I initialize it with the function -(void)login:(NSString *)username password:(NSString *)pw and pass it two UITextField.text strings.

If you notice in the init I am testing stuff with NSLog but at every breakpoint it seems like the storedLogins NSMutable array is nil.

IONServer.m

#import "IONServer.h"
#import "IONLoginResult.h"

@interface IONServer ()

@property (nonatomic) NSMutableArray *storedLogins;

@end

@implementation IONServer

-(void)createStoredLogins
{
    NSArray *firstUser = @[@"user1",@"pass1"];
    NSArray *secondUser = @[@"user2",@"pass2"];

    [self.storedLogins addObject:firstUser];
    [self.storedLogins addObject:secondUser];

}

-(instancetype)init {
    self = [super init];

    if (self) {
        [self createStoredLogins];
        NSLog(@"Stored logins: %@", _storedLogins);
        NSLog(@"Stored user: %@", _storedLogins[0][0]);
    }
    return self;

}

-(void)login:(NSString *)username password:(NSString *)pw
{
    NSArray *logins = [[NSArray alloc]initWithArray:_storedLogins];

    for (int i = 0; i < [logins count]; i++) {
        if (username == logins[i][0] && pw == logins[i][1]) {
            IONLoginResult *result = [[IONLoginResult alloc] initWithResult:YES errorMessage:@"Success!"];
            self.result = result;
            break;
        } else {
            IONLoginResult *result = [[IONLoginResult alloc] initWithResult:NO errorMessage:@"Error!"];
            self.result = result;
        }
    }
}

-(void)logout
{

}

@end
Was it helpful?

Solution

You need to initialize the array:

-(instancetype)init {
    self = [super init];

    if (self) {
        _storedLogins = [[NSMutableArray alloc] init];
        [self createStoredLogins];
        NSLog(@"Stored logins: %@", _storedLogins);
        NSLog(@"Stored user: %@", _storedLogins[0][0]);
    }
    return self;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top