OC8_NSData

//
//  main.m
//  OC8_NSData
//
//  Created by zhangxueming on 15/6/19.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *string = @"i love ios";
        //把字符串对象转换成NSData对象
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"data = %@", data);

        //把NSData类对象转换成字符串对象
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"str = %@", str);

        //把data对象数据写到指定文件中,文件不存在,则创建指定文件
        //- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
        BOOL ret = [data writeToFile:@"/Users/zhangxueming/Desktop/Test/data.txt" atomically:YES];
        NSLog(@"ret = %i", ret);
        //把文件内容读成二进制对象
        NSData *data1 = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/Test/data.txt"];
        NSLog(@"data1 = %@", data1);

    }
    return 0;
}
时间: 2024-10-13 03:19:30

OC8_NSData的相关文章