NSData資料型別的講解
也就是網路中要傳資料的的時候,我們通常會用相應的NSData的資料來進行儲存。
1、 NSData 與 NSString: //轉成UTF8的資料
NSString * str = @"hello, world!";
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
//NSString轉換成NSData型別
NSString * neswStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//將Data資料轉化為字串
2. NSData 與 UIImage
NSData->UIImage
UIImage *aimage = [UIImage imageWithData: imageData];
//例:從本地檔案沙盒中取圖片並轉換為NSData
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
// finalPath為獲取圖片的路徑,獲取圖片的資料
UIImage *aimage = [UIImage imageWithData: imageData];
//將圖片的資料轉化為圖片
3.NSData與NSArray NSDictionary
+(NSString *)getLocalFilePath:(NSString *) fileName
{
return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@“Documents”,fileName];
}
包括將NSData寫進Documents目錄,從Documents目錄讀取資料
在進行網路資料通訊的時候,經常會遇到NSData型別的資料。
在該資料是dictionary結構的情況下,系統沒有提供現成的轉換成NSDictionary的方法,為此可以通過Category對NSDictionary進行擴充套件,以支援從NSData到NSDictionary的轉換。
下面是在NSdictionary中新增轉化為NSData的方法。
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {
CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL);
if(list == nil) return nil;
if ([(id)list isKindOfClass:[NSDictionary class]]) {
return [(NSDictionary *)list autorelease];
}
else {
CFRelease(list);
return nil;
}
}
NSData生成:
NSDictionary *dic =[NSDictionary dictionaryWithObject:@"hello" forKey:@"KEY"];
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:dic];
從檔案生成NSData:
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"hello" ofType:@"png"];
NSData *d = [[NSData alloc] initWithContentsOfFile: path];
取得元素長度:
int i = [d length];
NSData型轉成NSDictionary型:
NSDictionary *reverse = [NSKeyedUnarchiver unarchiveObjectWithData: d];
4.NSData與NSDate
//函式作用 :date根據formatter轉換成string
+(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return[dateFormatter stringFromDate:date];
}
//函式作用 :string型別的時間戳轉換成時間
+(NSString*)dateStringToString:(NSString *)dateStr
{
if ([dateStr isKindOfClass:[NSNull class]]) {
return nil;
}
else{
NSInteger time = [dateStr intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];
return needtime;
}
}
//函式作用 :將日期從原格式轉換成需要的格式
+(NSString*)convertDateFormatter:(NSString*)sourceFormatter
targetFormatter:(NSString*)targetFormatter
dateString:(NSString*)dateString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:sourceFormatter];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:targetFormatter];
return[dateFormatter stringFromDate:date];
}
//函式作用 :將日期字串轉換成date
+(NSDate *)stringToDate:(NSString *)formatter dateString:(NSString *)dateString{
NSDateFormatter *dateFormatter= [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return [dateFormatter dateFromString:dateString];
}
//時間戳轉換成時間
NSInteger time = [[dic objectForKey:@"press_date"] intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];
sh