2011-07-04 7 views
0

이 코드에서 나는 txt 파일을 읽었습니다 :이 txt 파일은 다음과 같은 정보를 가지고 있습니다 : 하나 # 2 # three # four # 5; 하나 # 2 # three # four # 5;IOS : nsarray 레이아웃의 문제점

- (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"기호 및 어떻게 배열에서 제거합니까?

답변

0

텍스트 파일의 첫 번째 부분을 배열에 넣으시겠습니까? 나는 당신이 그 문제를 지나치게 복잡하게 만들고 있다고 생각합니다. 어떻게 이런 일에 대해 :

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 
) 

것을 실행하면이 당신이 원하는 무엇인가? 그렇지 않다면 더 잘 설명 할 수 있습니까?

+0

괜찮지 만 구성 요소 1과 구성 요소 2를 하나의 배열에 넣으려고합니다. – CrazyDev

+0

그래,이 질문에 분명하지 않습니다. 시도해 NSArray * arrayFromInputString = [inputString componentsSeparatedByCharactersInSet : [NSCharacterSet characterSetWithCharactersInString : @ "#;"]]]; – phi

+0

내가 nslog에있다 2011-07-04 10 : 44 : 54.378 project [331 : 707] arrayFromInputString :( "(\ n 0, \ n 1, \ n 11, \ n \"2011-07-04 08 : 39 : 08 +0000 \ ", \ n \"2011-07-14 00:00:00 +0000 \ ", \ n \"\ ", \ n 0, \ n 0, \ n (\ n) \ "\"\ "), " " ) – CrazyDev