2012-12-19 3 views
3
-(void)generateCSV 
{ 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

[filemgr createFileAtPath:@"test.csv" contents:nil attributes: nil]; 
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:@"test.csv"]; 

NSLog (@"why null %@", file); 
[file seekToEndOfFile]; 

NSLog(@"%@", file); 

surveyarray = [db retrieveSurvey]; 

for(int i = -1; i < surveyarray.count; i++) 
{ 
    if(i == -1) 
    { 
     NSString *str = [NSString stringWithFormat: @"No, CreatedDate, Question1, Question2, Question3, Comment\n"]; 
     [file writeData: [str dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; 
    } 
    else 
    { 
     survey = [surveyarray objectAtIndex:i]; 
     NSString *str = [NSString stringWithFormat: @"%d, %@, %@, %@, %@, %@\n", i, survey.createddate, survey.question1, survey.question2, survey.question3, survey.comment]; 
     [file writeData: [str dataUsingEncoding: NSUTF16LittleEndianStringEncoding]]; 
    } 
} 

[file closeFile]; 

/*CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:fullPath atomic:NO]; 

NSInteger numberOfColumns = 5; 

for (NSInteger currentIndex = 0; currentIndex < [surveyarray count]; currentIndex++) 
{ 
    id field = [surveyarray objectAtIndex:currentIndex]; 

    [csvWriter writeField:field]; 

    if ((currentIndex % numberOfColumns) == (numberOfColumns - 1)) 
    { 
     [csvWriter writeLine]; 
    } 
} 

[csvWriter release];*/ 
} 

je devais connecter le fichier et retourner (null)Impossible de générer un fichier csv

sortie du journal

2012-12-19 14:46:55.853 OCBC[6725:907] (null) 
2012-12-19 14:46:55.860 OCBC[6725:907] 2012-12-19 14:14:31 
2012-12-19 14:46:55.862 OCBC[6725:907] Interview 
2012-12-19 14:46:55.863 OCBC[6725:907] Visitor 
2012-12-19 14:46:55.865 OCBC[6725:907] Meet Expectation 
2012-12-19 14:46:55.866 OCBC[6725:907] N/A 
+1

Je pense que votre chemin paramètre est faux .. – Stas

+0

@Stas, Quel est le paramètre correct? Y at-il un exemple qui crée un nouveau fichier dans l'iphone ou l'ipad? –

+0

ASTUCE: createFileAtPath: signifie que vous avez besoin d'un chemin d'accès complet, pas seulement un nom de fichier. –

Répondre

1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDir stringByAppendingPathComponent:@”test.csv”]; 

essayer de passer la filePath à votre méthode

+0

J'ai essayé ceci et notez le 'documentsDir', il renvoient null aussi –

+0

' [filemgr currentDirectoryPath] 'retour nul –

Questions connexes