如何从函数写入.csv文件?

嘿朋友,我通过使用这个function得到了我的iphone的所有联系细节..

-(void)collectContacts { ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); //NSLog(@" the Name %@%",people); for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++) { ABRecordRef ref = CFArrayGetValueAtIndex(people, i); // Get First name, Last name, Prefix, Suffix, Job title NSString *firstName = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty); NSString *lastName = (NSString *)ABRecordCopyValue(ref,kABPersonLastNameProperty); NSString *prefix = (NSString *)ABRecordCopyValue(ref,kABPersonPrefixProperty); NSString *suffix = (NSString *)ABRecordCopyValue(ref,kABPersonSuffixProperty); NSString *jobTitle = (NSString *)ABRecordCopyValue(ref,kABPersonJobTitleProperty); NSLog(@" the phoneType %@%",firstName); NSLog(@" the phoneType %@%",lastName); ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty); for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) { CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j); NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex(phones, j)); NSString *phoneNumber = (NSString *)phoneNumberRef; NSLog(@" the phoneType %@%",phoneLabel); NSLog(@" the phoneNumber %@%",phoneNumber); } CFStringRef address; CFStringRef label; ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonAddressProperty); for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) { label = ABMultiValueCopyLabelAtIndex(multi, i); CFStringRef readableLabel = ABAddressBookCopyLocalizedLabel(label); NSLog(@" Lable %@%",readableLabel); address = ABMultiValueCopyValueAtIndex(multi, i); NSLog(@" Address %@%",address); CFRelease(address); CFRelease(label); } ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty); for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++) { CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, idx); NSString *strLbl = (NSString*) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex (emails, idx)); NSLog(@" Email Lable %@%",strLbl); NSString *strEmail_old = (NSString*)emailRef; NSLog(@" Email-Id %@%",strEmail_old); //[[strEmail_old componentsJoinedByString:@","] writeToFile:@"components.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; } } //[[ContactsText componentsJoinedByString:@","] writeToFile:@"components.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; // NSLog(@" Data in the .CSV file = %@%",ContactsText); } ' 

现在我想从这个函数的输出.csv文件..和m获取NSLog中的所有数据。 我juat想要从该数据写入.csv文件..任何人都可以帮助我? 数据来是正确的,但.CSV文件不会写…谢谢

假设您将拥有“ContactsText”中的所有数据,请尝试以下代码:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* savePath = [paths objectAtIndex:0]; savePath = [savePath stringByAppendingPathComponent:@"components.csv"]; [[ContactsText componentsJoinedByString:@","] writeToFile:savePath atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 

请记住,您不能保存“components.csv”文件。 您只需将文件保存在文档文件夹中。

有许多开源扩展可以为数组添加csv支持。 例如在GitHub上可用的CHCSVParser 。

请注意,您的数据可能包含需要特殊处理的字符 – 对于CSV文件,最明显是逗号本身。 如果您写出的string中有一个逗号,则应该在该字段周围放置双引号。 在这种情况下,如果string中已经有一个双引号,那么也需要将其转义。

我build议你使用Dave DeLong的CHCSVParser ,你可以在这里find它: http : CHCSVParser

导入一个良好的第三方框架以确保边缘案例得到正确处理是值得的。