NSDate 获取当前日期及时间 [NSDate date]获取的时间是标准时区的时间
转换成系统时间:
NSDate *date = [NSDate date];
NSTimeZone *zone=[NSTimeZone systemTimeZone];
NSTimeInterval timeInt=[zone secondsFromGMTForDate:date];
date = [date dateByAddingTimeInterval:timeInt];
NSString 转换成NSData 对象:
NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
NSData 转换成NSString对象:
NSData * data;
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData 转换成char*
NSData *data;
charchar *test=[data bytes];
char* 转换成NSData对象
byte* tempData = malloc(sizeof(byte)*16);
NSData *content=[NSData dataWithBytes:tempData length:16];
NSString和NSURL 转换
//NSString->NSURL
NSString *urlString=[@"http://www.google.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:urlString];
//NSURL->NSString
NSString *urlString=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString和NSData转换(即可作json串互相转换)
//NSData-> NSString
NSString *testString = [[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding];
//NSString->NSData
NSString *aString = @"我是NSString";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
NSData和NSImage转换
//NSData->UIImage
UIImage *testImage = [UIImage imageWithData: imageData];
//UIImage->NSData
NSData *imageData = UIImagePNGRepresentation(testImage)
//或者
NSData *imageData = UIImageJPEGRepresentation(testImage,1.0)
NSData和NSDictionary转换
//NSData-->NSDictionary
NSDictionary *testDict = [NSJSONSerialization JSONObjectWithData:testData options:NSJSONReadingMutableContainers error:nil]
//或者
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:testData];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
[unarchiver finishDecoding];
//NSDictionay --> NSData
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:params forKey:@"Some Key Value"];
[archiver finishEncoding];
NSDictionary和JSON转换
JSON: //这个方法是SBJson类库里的,需要导入SBJson.h
NSString *jsonStr=[dict JSONRepresentation];
//或者原生类库中
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//建议(特别是http取回来的jsonStr转换如下)
//JSON->NSDictionary:
NSData *jsonData = [testJsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]
IOS 系统API---NSJSONSerialization四个枚举什么意思
NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NSMutableArray。
NSJSONReadingMutableLeaves:返回的JSON对象中字符串的值为NSMutableString,目前在iOS 7上测试不好用,应该是个bug,参见:
http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working
NSJSONReadingAllowFragments:允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。参见:
http://stackoverflow.com/questions/16961025/nsjsonserialization-nsjsonreadingallowfragments-reading
NSJSONWritingPrettyPrinted:的意思是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。
NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象(参考下面链接)
http://my.oschina.net/u/1245365/blog/294449