减少long for / if语句的长度

时间:2014-07-10 08:35:55

标签: objective-c arrays

我目前正在尝试制作应用程序的代码,我的开发效率更高,更易于阅读。基本上它的作用是从玩家名称的NSUserDefaults中检索一个数组,并用这些名称填充6个文本框(标记为6-11)。如果没有现有数组,它将使用另一组名称。任何简化此代码的想法都将受到赞赏。

if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        NSMutableArray *names = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"nameArray"]];
        for (int i = 0; i <= 5; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            UITableViewCell *cell = [playerTable cellForRowAtIndexPath:indexPath];

            for (UIView *view in cell.contentView.subviews) {
                if ([view isKindOfClass:[UITextField class]]) {
                    UITextField *txtField = (UITextField *)view;
                    if (txtField.tag == 6) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:0]; }
                        else {
                            txtField.text = @"Peter";
                        }
                    }
                    if (txtField.tag == 7) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:1]; }
                        else {
                            txtField.text = @"Julia";
                        }
                    }
                    if (txtField.tag == 8) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:2]; }
                        else {
                            txtField.text = @"Durgan";
                        }
                    }
                    if (txtField.tag == 9) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:3]; }
                        else {
                            txtField.text = @"Bob";
                        }
                    }
                    if (txtField.tag == 10) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:4]; }
                        else {
                            txtField.text = @"Iseland";
                        }
                    }
                    if (txtField.tag == 11) {
                        if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
                            txtField.text = [names objectAtIndex:5]; }
                        else {
                            txtField.text = @"Player";
                        }
                    }
                }
            }
        }
        [self saveNames];
    }

2 个答案:

答案 0 :(得分:5)

你可以这样做:

NSArray *defaultNames = @[@"Peter", @"Julia",...];
int offsetIndex = 6;
BOOl needCustomNames = [[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"];
for (UIView *view in cell.contentView.subviews)
{
    if ([view isKindOfClass:[UITextField class]]) 
    {
        UITextField *txtField = (UITextField *)view;
        int index = [txtField tag]-offsetIndex;
        if (tag >= 6 && tag <= 11)
        {
            if (needCustomNames)
                textField.text = [names objectAtIndex:index];
            else
                textField.text = [defaultNames objectAtIndex:index];
        }
    }
}

例如,您在for循环中调用过多次相同的行,例如检查是否需要使用自定义名称(NSUserDefaults行)。
我使用NSArray作为自定义名称来模仿相同的逻辑,并使用偏移量来澄清它。

答案 1 :(得分:1)

我建议您分析您的代码以获得冗余,并将其排除在单独的方法中。所以你必须一次又一次地调用extern方法而不是每个代码方。