每日一练:OC中Write NSString to File

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...

//        NSMutableString *str = [[NSMutableString stringWithUTF8String:"I have a apple"] init];

        NSMutableString *str = [[NSMutableString alloc] init];

        for(int i = 0; i < 10; i++) {

            [str appendString:@"I have a apple, Nice One! !\n"];

            

            NSLog(@"the data is %@", str);

            NSLog(@"the num is %d",i);

        }

        NSError *err;

        BOOL success = [str writeToFile:@"/Users/xxxx/xxxx/test/cool.txt"

                             atomically:YES

                               encoding:NSUTF8StringEncoding

                                  error:&err];

        if(success) {

            NSLog(@"Writing file is successful!");

        } else {

            NSLog(@"Write file is failed, with error : %@", [err localizedDescription]);

        }

    }

    return 0;

}

Result:

2018-03-13 19:19:54.668894+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

2018-03-13 19:19:54.669153+0800 TOCFileHandlinga[58008:3488593] the num is 0

2018-03-13 19:19:54.669174+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.669187+0800 TOCFileHandlinga[58008:3488593] the num is 1

2018-03-13 19:19:54.669244+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.669262+0800 TOCFileHandlinga[58008:3488593] the num is 2

2018-03-13 19:19:54.669277+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.669291+0800 TOCFileHandlinga[58008:3488593] the num is 3

2018-03-13 19:19:54.669305+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.669318+0800 TOCFileHandlinga[58008:3488593] the num is 4

2018-03-13 19:19:54.669331+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.673562+0800 TOCFileHandlinga[58008:3488593] the num is 5

2018-03-13 19:19:54.673606+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.673625+0800 TOCFileHandlinga[58008:3488593] the num is 6

2018-03-13 19:19:54.673636+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.673652+0800 TOCFileHandlinga[58008:3488593] the num is 7

2018-03-13 19:19:54.674036+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.674088+0800 TOCFileHandlinga[58008:3488593] the num is 8

2018-03-13 19:19:54.674113+0800 TOCFileHandlinga[58008:3488593] the data is I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

I have a apple, Nice One! !

2018-03-13 19:19:54.674132+0800 TOCFileHandlinga[58008:3488593] the num is 9

2018-03-13 19:19:54.682996+0800 TOCFileHandlinga[58008:3488593] Writing file is successful!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值