自定义对象初始化不存储带有addObject的NSMutableArray对象: [英] Custom Object Initialization does not store NSMutableArray objects with addObject:

查看:90
本文介绍了自定义对象初始化不存储带有addObject的NSMutableArray对象:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建带有静态登录名的登录屏幕时,我试图将其私有存储在以下类实现中.当按钮创建IONServer对象时,我使用函数-(void)login:(NSString *)username password:(NSString *)pw对其进行初始化,并向其传递两个UITextField.text字符串.

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.

如果您在初始化中注意到我正在使用NSLog测试东西,但是在每个断点处,似乎storedLogins NSMutable数组是nil.

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

推荐答案

您需要初始化数组:

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

}

这篇关于自定义对象初始化不存储带有addObject的NSMutableArray对象:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆