Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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
share|improve this question
    
Yes, it is nil, because you're not assigning anything else to that variable; it starts nil, as all objective-C variables do. – Jesse Rusak Apr 27 '14 at 0:03

1 Answer 1

up vote 1 down vote accepted

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;

}
share|improve this answer
    
doh! That was it. Perfect! – Macness Apr 27 '14 at 0:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.