IOS: проблема с компоновкой nsarray - PullRequest
0 голосов
/ 04 июля 2011

В этом коде я читаю файл txt: этот файл txt содержит информацию таким образом: один # два # три # четыре # пять; один # два # три # четыре # пять;

- (void) readFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"File.txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

if (!fileString) {
    NSLog(@"Error reading file.");
} 

NSMutableArray *itemDB = [[fileString componentsSeparatedByString:@";"] mutableCopy]; 
[itemDB removeLastObject];

NSMutableArray *returnArray = [[NSMutableArray alloc] init];

for (int i = 0; i<[itemDB count]; i++) {

    NSArray *datiItemDB = [[itemDB objectAtIndex:i] componentsSeparatedByString:@"#"];
    [returnArray addObject: datiItemDB]; 
}

one = [[NSMutableArray alloc] initWithArray:(NSArray*)returnArray]; 

[returnArray release];
}

результат nslog этого массива равен

2011-07-04 10:01:16.847 Project[254:707] array one:(
    (
    "(\n    0,\n    1,\n    12,\n    \"2011-07-04 08:00:41 +0000\",\n    \"2011-07-15 00:00:00 +0000\",\n    \"\",\n    0,\n    0,\n        (\n    ),\n    \"\"\n)"
)
)

, но я хочу, чтобы результат nslog был

2011-07-04 09:52:07.281 Project[230:707] array one:(
    (
    0,
    1,
    1,
    "2011-07-04 07:45:15 +0000",
    "2011-07-04 07:45:15 +0000",
    "",
    0,
    0,
            (
    ),
    ""
)
)

Почему у меня есть символы "/ n" и как я могу его удалитьиз моего массива?

1 Ответ

0 голосов
/ 04 июля 2011

Вы просто хотите поместить первую часть текстового файла в массив?Я думаю, что вы слишком усложняете проблему.Как насчет чего-то вроде этого:

NSString *inputString = @"one#two#three#four#five;one1#two2#three3#four4#five5;";
NSArray *arrayFromInputString = [inputString componentsSeparatedByString:@";"];
NSArray *component1 = [[arrayFromInputString objectAtIndex:0] componentsSeparatedByString:@"#"];
NSLog(@"1: %@", [component1 description]);
NSArray *component2 = [[arrayFromInputString objectAtIndex:1] componentsSeparatedByString:@"#"];
NSLog(@"2: %@", [component2 description]);

Если вы запустите это, вывод будет

[777:207] 1: (
    one,
    two,
    three,
    four,
    five
)
[777:207] 2: (
    one1,
    two2,
    three3,
    four4,
    five5
)

Это то, что вы хотели?Если нет, можете ли вы попытаться объяснить лучше?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...