`- (Lock *)lock
{
if (!_lock){
_lock = [[Lock alloc] init];
_seqIndex = 0;
_confIndex = 0;
_confirmArray = [[NSMutableArray alloc] init];
}
return _lock;
}`
`- (IBAction)touchPicturePoint:(UIButton *)sender
{
NSNumber *chosenPicturePoint = [NSNumber numberWithUnsignedInteger:[self.picturePoints indexOfObject:sender]];
if (self.newLock){
if (self.seqIndex < MAXSEQUENCE){
[self.lock setLock:chosenPicturePoint
:self.username];
self.seqIndex ++;
if (self.seqIndex == MAXSEQUENCE){
self.userPrompt.text = @"Please Confirm Your PicturePass";
}
} else {
if (self.confIndex < MAXSEQUENCE){
self.confirmArray[self.confIndex] = chosenPicturePoint;
self.confIndex++;
}
if (self.confIndex == MAXSEQUENCE){
if ([self.lock confirmLock:self.confirmArray
:self.username]){
self.lock = nil;
[self performSegueWithIdentifier:@"Register Success" sender:sender];
} else {
self.userPrompt.text = @"PicturePass Mismatch! Please try again!";
[self.lock eraseLockFromUser:self.username];
self.lock = nil;
}
}
}
}
if (!self.newLock){
if (self.confIndex < MAXSEQUENCE){
self.confirmArray[self.confIndex] = chosenPicturePoint;
self.confIndex++;
}
if (self.confIndex == MAXSEQUENCE){
if ([self.lock confirmLock:self.confirmArray
:self.username]){
[self performSegueWithIdentifier:@"Login Success" sender:sender];
} else {
self.userPrompt.text = @"Invalid PicturePass, try again!";
self.lock = nil;
}
}
}
}`
在上面的代码中,我遇到了一个问题,在“if (!self.newLock){” block 中,它将一个空数组传递给 [self.lock confirmLock],但是在“if (self.newLock)”中,我具有看似相同的代码,但它按预期传递数组。
confirmLock 的代码是:
`- (BOOL)confirmLock:(NSArray *)confirmArray
:(NSString *)username
{
if (![confirmArray count]){
NSLog(@"WTF");
abort();
}
NSManagedObjectContext *context = [self managedObjectContext];
Login *user = [self fetchUser:username
:context];
if (([user.point1 integerValue] == [confirmArray[0] integerValue])&&([user.point2 integerValue] == [confirmArray[1] integerValue])&&([user.point3 integerValue] == [confirmArray[2] integerValue])&&([user.point4 integerValue] == [confirmArray[3] integerValue])){
return YES;
}
return NO;
}`
最佳答案
标准开场白:参数应该在 Objective-C 中命名。不给它们命名被认为是糟糕的编码。
抛开家长式的评论,这个:
self.lock confirmLock:self.confirmArray
:self.username
有效地变成:
objc_msgSend(self.lock, @selector(confirmLock::), self.confirmArray, self.username);
在 C 语言中,函数的参数没有定义的求值顺序。因此,在 Objective-C 中,对于方法的参数或用于发送消息的任何 getter 都没有定义的求值顺序。
在你的例子中,self.confirmArray
可以在 self.lock
之前调用.所以你会通过 nil
.作为[nil <anything>]
计算结果为零,[nil count]
计算结果为零。
编辑:而且,根据上面 CrimsonChris 的评论,您似乎还故意创建了一个空数组。因此,即使它通过了,随后它还是空的也就不足为奇了。
关于ios - 将 NSArray 传递给函数的 Objective-C 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973951/