如何从函数中编写.csv文件?

时间:2011-08-23 09:06:36

标签: iphone ios cocoa-touch file-upload csv

嘿朋友我通过使用此功能获得了我的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文件..并获取NSLog中的所有数据。我想要从那些数据写出.csv文件..任何人都可以帮我吗?数据即将推出,但.csv文件不会写...谢谢

3 个答案:

答案 0 :(得分:4)

假设您将拥有“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”文件。您只需将文件保存在文档文件夹中。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

我建议你使用Dave DeLong的CHCSVParser,你可以在这里找到它:http://davedelong.com/portfolio

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