@fiy-fish
2015-07-18T14:04:57.000000Z
字数 1135
阅读 2088
Objective-c
#if 0
//NSData的使用 也就是对把对象和基本数据类型的内容转换成二进制数据流
int main(int argc, const char * argv[]) {
@autoreleasepool {
//桌面路径
NSString * path = [NSHomeDirectory() stringByAppendingString:@"Desktop/f1.txt"];
NSString * temp = @"Hello Friend";
NSArray * array = @[@"hello",@"world",@1];
int i = 100;
float f = 98.3333f;
NSMutableData * writer = [[NSMutableData alloc] init];
//给data 添加内容
//1.添加data类元素
/* - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
此方法只能由NSString 对象来调用
*/
[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
/*
- (void)appendBytes:(const void *)bytes length:( NSUInteger)length;
这个方法用来给data 添加基本数据类型
*/
[writer appendBytes:&i length:sizeof(i)];
[writer appendBytes:&f length:sizeof(f)];
//将数据写入到路径文件中
[writer writeToFile:path atomically:YES];
//下面来读取data数据
int ii;
float ff;
NSString * ttemp;
//读取文件中得数据
NSData *reader = [NSData dataWithContentsOfFile:path];
//
ttemp = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding];
[reader getBytes:&ii range:NSMakeRange([temp length], sizeof(ii))];
[reader getBytes:&ff range:NSMakeRange([temp length] + sizeof(ii), sizeof(ff))];
NSLog(@"String:%@ int:%i float:%f", ttemp, ii, ff);
}
return 0;
}
#endif