温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

写入文件 (字符串/ 数组 / 字典)

发布时间:2020-06-18 05:48:28 来源:网络 阅读:205 作者:缘起愿落 栏目:开发技术

    获取文件路径
- (NSString *)getFilePath
{
         2.获取所要存储的文件路径
           (1)获取Documents文件夹路径
             NSDocumentDirectory 用来获取指定文件夹的路径
             NSUserDomainMask 设置查找的域,我们的自己的文件都是存储在用户域的
             @param yes 是否使用详细路径(绝对路径)
             @return 因为最初该方法是适用于MAC OS的,而对于电脑系统可能有对个用户,所以获取到的路径可能有多条,所以返回值类型是数组,但对于iOS下,就只有一个用户,所以数组中只有一个元素.
        

    NSString * documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
           

            (2)拼接上要存储的文件路径.
    NSString * newFilePath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
    return newFilePath;
}



     1.获取存储的内容.
    NSString * content = self.fileview.TF.text;
    NSString * content1 = self.fileview.TFView.text;
     

    3.将内容存储到指定文件路径
    NSError * error = nil;
    
        (1)字符串写入本地文件
             BOOL isSucceed = [content writeToFile:[self getFilePath] atomically:YES encoding:NSUTF8StringEncoding error:&error];
               NSLog(@"%d",isSucceed);

        (2)数组写入本地文件.
              NSArray * arr = @[content,content1];
               BOOL isSucceed = [arr writeToFile:[self getFilePath] atomically:YES];
               NSLog(@"%d",isSucceed);
    
         (3)字典写入本地文件
                NSDictionary * dic = @{@"tf1": content, @"tf2":content1};
               BOOL isSucceed = [dic writeToFile:[self getFilePath] atomically:YES];
                 NSLog(@"%d",isSucceed);


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI