2012-05-13 6 views
4

CHCSVParser을 사용하여 코어 데이터를 CSV로 내보내겠습니다. 엔티티에서 모든 가치를 얻는 방법을 알고 있지만 CSV에 쓰는 방법을 모르겠습니다.코어 데이터를 CSV로 내보내는 방법

누구나 CSV에 쓸 방법을 가르쳐 줄 수 있습니까? CHCSVParser?

// Test listing all Infos from the store 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription 
           entityForName:@"NoteLog" inManagedObjectContext:context]; 
[fetchRequest setEntity:entity]; 

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
for (NoteLog *noteInfo in fetchedObjects) { 

    NSLog(@"Name: %@", noteInfo.city); 
    NSLog(@"Name: %@", noteInfo.country); 
    NSLog(@"Name: %@", noteInfo.datetime); 
    NSLog(@"Name: %@", noteInfo.notelatitude); 
    NSLog(@"Name: %@", noteInfo.notelongtitude); 
    NSLog(@"Name: %@", noteInfo.state); 
    NSLog(@"Name: %@", noteInfo.text);   
} 

답변

8

CHCSVWriter는 CSV 파일을 구성하는 여러 가지 방법이 있습니다

-writeField:는 객체를 허용하고 CSV 파일에서 (탈출 제대로 된 후)의 -description를 기록합니다. 필요한 경우 필드 분리 자 (,)를 씁니다. 빈 문자열 (@ "") 또는 nil을 전달하여 빈 필드를 작성할 수 있습니다.

-writeFields:은 쉼표로 구분 된 nil 종결 개체 목록을 허용하고 각 개체를 -writeField :로 보냅니다.

-writeLine은 현재 CSV 라인을 종료하는 데 사용됩니다. -writeLine을 호출하지 않으면 모든 CSV 필드가 한 줄에 표시됩니다.

-writeLineOfFields:은 쉼표로 구분되고 nil로 끝나는 개체 목록을 허용하고 각 개체를 -writeField :로 보낸 다음 -writeLine을 호출합니다.

-writeLineWithFields:은 개체 배열을 허용하고 각 개체를 -writeField :로 보낸 다음 -writeLine을 호출합니다.

-writeCommentLine: 문자열을 받아 들여 CSV 스타일 주석으로 파일에 기록합니다.

CHCSVWriter은 파일에 쓰는 것 외에도 NSString에 직접 쓸 수 있도록 초기화 할 수 있습니다.

무언가가 당신을 위해 작동해야합니다.

+0

Rayan 안녕하세요, 당신이 나에게 예를 제공하는 beable 것인가? 내가 필요한 모든 데이터를 내보내려면 그게 전부 – Desmond

+2

특정 사건에 대한 몇 가지 코드를 추가했습니다. 그걸 붙여 넣으면 콘솔에서 멋진 CSV 출력을 얻어야합니다. 거기에서 나는 그것을 당신이 그것을 파일에 넣는 방법을 알고 그것을 가지고 무엇을 하든지 할 수 있다고 믿습니다. –

+0

굉장한 감사 Rayan. 완벽하게 작동합니다. – Desmond

8

위의 대답은 더 이상 사용되지 않는 것으로 보입니다. 작성자는이 방법을 다른 것으로 바 꾸었습니다. 이것은 나를 위해 일한 , 그것은 도움이되기를 바랍니다 :

NSOutputStream *stream = [[NSOutputStream alloc] initToMemory]; 
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:stream encoding:NSUTF8StringEncoding delimiter:',']; 

for (Type *instance in fetchedResults) { 
    [writer writeLineOfFields:@[instance.propertyA, instance.B]]; 
} 
[writer closeStream]; 

NSData *buffer = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; 
NSString *output = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding]; 
+0

fetchedResults 란 무엇입니까? –

+0

@LukeIrvin, 데이터베이스 엔터티의 배열입니다. 원래 게시물을 참조하십시오 - NSArray * fetchedObjects = [context executeFetchRequest : fetchRequest error : & error]; –

+0

'writeLineOfFields'를 신속하게 사용하는 방법에 대한 아이디어가 있으십니까? – Steven

관련 문제