Objective-C错误将NSArray传递给函数

时间:2014-04-09 21:07:10

标签: ios objective-c nsarray

   `- (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){”块中它将一个空数组传递给[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;
}`

2 个答案:

答案 0 :(得分:2)

标准开放注释:参数应在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上面的评论,你似乎也故意创建一个空数组。因此,即使它已经通过,它随后也是空的并不奇怪。

答案 1 :(得分:0)

你的方法confirmLock带有一个未命名的参数,用name改变它的参数。你可以尝试这个。 例如:

  • (BOOL)confirmLock:(NSArray *)confirmArray withname:(NSString *)username