以下将输出一个NSMutableArray
数组,每个数组都包含您提供的字符串数。因此,如果字符串数为 3,则每个数组将有 3 个字符串,除非没有剩余字符串,在这种情况下它将添加尽可能多的字符串。
int numLines = 4;
int numWordsPerLine = 3;
NSArray *strings = @[@"String1", @"String2", @"String3", @"String4", @"String5", @"String6", @"String7", @"String8"];
NSMutableArray *matrixArray = [NSMutableArray arrayWithCapacity:numLines];
NSEnumerator *enuma = [strings objectEnumerator];
int linesCount = 0;
while (linesCount < numLines) {
[matrixArray addObject:[NSMutableArray array]];
for (int i = 0; i < numWordsPerLine; i++) {
id nextObject = [enuma nextObject];
if (nextObject) {
[[matrixArray objectAtIndex:linesCount] addObject:nextObject];
}
}
linesCount++;
}
NSLog(@"Matrix %@", matrixArray);
那将输出:
2013-11-12 11:42:06.940 WordPuzzle[1900:a0b] Matrix (
(
String1,
String2,
String3
),
(
String4,
String5,
String6
),
(
String7,
String8
),
(
)
)
如果您更改numLines
为 2,然后更改numWordsPerLine
为 4,您将获得:
2013-11-12 11:43:39.014 WordPuzzle[1936:a0b] Matrix (
(
String1,
String2,
String3,
String4
),
(
String5,
String6,
String7,
String8
)
)