如何从函数写.csv文件? [英] how to write .csv file from function?

查看:160
本文介绍了如何从函数写.csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友我通过使用这个功能获得我的iphone的所有联系人详细信息..

Hey Friends i got all contacts detail of my iphone by using this function ..

    -(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。我juat想从该数据写.csv文件..任何人都可以帮助我在那?数据来了是正确的但是.csv文件不会写...感谢

Now i want to make .csv file from this function's output .. and m getting all data in NSLog . I juat want to write .csv file from that data .. Can anyone help me in that ?? Data coming are proper but .csv file not gonna written ...Thanks

推荐答案

假设你将 ,尝试以下代码:

Assuming you will have all the data in "ContactsText", try following code:

    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文件。您必须将文件保存在文档文件夹中。

Remember that you cannot just save "components.csv" file. You have to save the file in documents folder only.

这篇关于如何从函数写.csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆