如何在目标c中以期望的格式显示字符串组合

时间:2013-01-10 14:02:23

标签: iphone objective-c

您好我想在nslog上显示字符串组合,如此格式

abc
bca
acb
and so on 

但 我的程序告诉我它就像这种格式 排列=

 (
        (
        c,
        b,
        a
    ),
        (
        b,
        c,
        a
    ),
        (
        c,
        a,
        b
    ),
        (
        a,
        c,
        b
    ),
        (
        b,
        a,
        c
    ),
        (
        a,
        b,
        c
    )
)

这是我正在使用的代码

NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];

NSMutableArray *permutations = nil;

int i = 0;
for (i = 0; i < array.count ; i++){

    if (!permutations){
        permutations = [NSMutableArray array];
        for (NSString *character in array){
            [permutations addObject:[NSArray arrayWithObject:character]];


        }

    } else {

        //make copy of permutations array and clean og array
        NSMutableArray *aCopy = [permutations copy] ;
        [permutations removeAllObjects];

        for (NSString *character in array){

            //loop through the copy
            for (NSArray *oldArray in aCopy){

                //check if old string contains looping char..
                if ([oldArray containsObject:character] == NO){

                    //update array
                    NSMutableArray *newArray = [NSMutableArray arrayWithArray:oldArray];
                    [newArray addObject:character];

                    //add to permutations
                    [permutations addObject:newArray];

                }

            }
        }            
    }



}

NSLog(@"permutations = \n %@",permutations);
}

请告诉我如何以我的要求格式显示它

4 个答案:

答案 0 :(得分:2)

尝试这种方式:

NSString *stringPermuted=[NSString new];
for (NSArray *array in permutations) {
    for (NSString *string in array) {
        stringPermuted=[stringPermuted stringByAppendingFormat:@"%@",string];
    }
    stringPermuted=[stringPermuted stringByAppendingFormat:@"\n"];
}

NSLog(@"permutations=\n%@",stringPermuted);

另一种方式:

NSString *stringPermuted=[NSString new];
for (NSArray *array in permutations) {
    stringPermuted=[stringPermuted stringByAppendingFormat:@"%@\n",[array componentsJoinedByString:@""]];
}
NSLog(@"permutations=\n%@",stringPermuted);

答案 1 :(得分:1)

创建一个简单的显示方法

- (NSString *)formatPermutations:(NSArray *)permutations {
    NSString * formattedPermutations = @"";
    for (NSArray * permutation in permutations) {
        formattedPermutations = [formattedPermutations stringByAppendingFormat:@"%@\n", [self formatPermutation:permutation]];
    }
    return formattedPermutation;
}

- (NSString *)formatPermutation:(NSArray *)permutation {
    NSString * formattedPermutation = @"";
    for (NSString * letter in permutation) {
        formattedPermutation = [formattedPermutation stringByAppendingString:letter];
    }
    return formattedPermutation;
}

并使用它

NSLog(@"permutations = \n %@",[self formatPermutations:permutations]);

另一个选项(也许是可取的选项)是创建自己的类PermutationArrayPermutation并覆盖其description方法。

这种方法相当于Java toString,只要NSLog需要从对象中获取NSString表示,就会调用它。

答案 2 :(得分:0)

使用最后一行NSLog(@"permutations = \n %@",permutations);,您将在控制台中记录整个NSMutableArray。 Xcode将其格式化为使其更具可读性。

尝试以这种方式记录您的结果:

更新版本:

for (NSArray *permutation in permutations){
    NSMutableString *tempString = [[NSMutableString alloc] initWithString:@""];
    for(NSString *character in permutation){
        [tempString appendString:character]
    }
    NSLog(@"%@\n",tempString);
}

答案 3 :(得分:0)

您的permutations对象是NSArray的NSArray。使用NSLog进行日志记录时,NSLog将调用NSArray的description方法,并使用换行符拆分数组中的对象。

如果你想改变它的打印方式,你可以使用自己的NSArray子类并覆盖描述方法,或者只写一个稍微复杂的日志语句,如下所示:

快速和肮脏

for (NSArray *arrayOfStrings in permutations) {
    for (NSString *oneCharacterString in arrayOfStrings) {
        printf("%s", [oneCharacterString UTF8String]);
    }
    printf(" ");
}

少一点不那么快就脏了

// with linebreaks after each permutation
// If you don't want those, move the NSLog out of the for loop construct one big string to log in the format you like
for (NSArray *arrayOfStrings in permutations) {
    NSMutableString *permutationString = [NSMutableString string];
    for (NSString *oneCharacterString in arrayOfStrings) {
        [permutationString appendString:oneCharacterString]
    }
    NSLog(permutationString);
}

其他评论:如果你只想保存一个字符,你也可以使用NSNumber对象(使用[NSNumber numberWithChar:]创建它)。 或者您可以使用NSString或NSMutableString而不是内部数组。