1

嗨,我想像这种格式在 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

4 回答 4

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);
于 2013-01-10T14:16:05.377 回答
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从对象中获取表示,就会调用它。

于 2013-01-10T14:17:41.767 回答
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);
}
于 2013-01-10T14:15:24.567 回答
0

您的排列对象是 NSArrays 的 NSArray。当使用 NSLog 记录它时,NSLog 将调用descriptionNSArray 的方法,这使用换行符来分割数组中的每个对象。

如果你想改变它的打印方式,你可以使用你自己的 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 代替内部数组。

于 2013-01-10T14:20:36.930 回答