[英]Custom Object Initialization does not store NSMutableArray objects with addObject:
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. 当按钮创建
IONServer
对象时,我使用-(void)login:(NSString *)username password:(NSString *)pw
对其进行初始化,并向其传递两个UITextField.text
字符串。
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
. 如果您在初始化中注意到我正在使用NSLog测试东西,但是在每个断点处,似乎
storedLogins
NSMutable数组为nil
。
IONServer.m 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
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.