Swift:在Documents Directory中创建csv文件

时间:2016-01-20 21:29:52

标签: ios swift csv nsfilemanager nsfilehandle

我正在创建一个注册表单应用程序。注册表格数据将保存在CSV文件中。我需要在文档目录中创建一个csv文件,并能够写入它。我对Swift很新,所以我遇到了一些麻烦。这是我到目前为止所得到的。

fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:event];

显然,我遇到问题的代码是FileHandle,它允许修改文件。在objective-c中,它看起来像这样:

[fileHandle seekToEndOfFile];
[fileHandle writeData:[formData dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

然后很明显,如果我想写文件,我可以这样做:

#import "Listing_Ads.h"

@implementation Listing_Ads


#pragma mark -
#pragma mark Properties Synthesized

@synthesize     title_value =   _title_value;
@synthesize     desc_value  =   _desc_value;
@synthesize     location    =   _location;
@synthesize     amount      =   _amount ;
@synthesize     date        =   _date;
@synthesize     image       =   _image;



#pragma mark -
#pragma mark Data_Allocation

- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];

    if (self)
    {
        NSLog(@" Titles are:%@",[dictionary valueForKey:@"title"]);
        self.title_value    =   [dictionary  valueForKey:@"title"];
        self.desc_value     =   [dictionary  valueForKey:@"description"];
        self.location       =   [dictionary  valueForKey:@"city_name"];
        self.amount         =   [dictionary  valueForKey:@"amount"];
        self.image          =   [dictionary  valueForKey:@"my_images"];
    }

    return self;

}

@end

所以我真的只是在努力解决那一部分问题。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

在Swift中,fileHandleForUpdatingAtPath已成为NSFileHandle的初始化程序,采用Swift命名约定,避免重复并取结束Objective-C方法名称,使其成为参数名称:

let fileHandle = NSFileHandle(forUpdatingAtPath: yourPath)

答案 1 :(得分:0)

确保在 info.plist 中添加这两个键并将它们设置为“YES

支持就地打开文档

应用支持iTunes文件共享