1.序列化与编码
编码与序列化:
有需要要保存自定义的class,NSUserDefault不能存储,只能通过编码与序列化的方式后存储到userdefault。
编码:class—>nsdata,序列化:nsdata->clas.自定义的类必须实现NSCoding协议。实现该协议的两个方法,如下:
@interface labelContentList : NSObject<NSCoding> //酒店标签列表
@property (nonatomic,retain) NSString *labelName;
@property (nonatomic,retain) NSString *labelType;
@property (nonatomic,retain) NSString *labelId;
@end
@implementation labelContentList
@synthesize labelId;
@synthesize labelName;
@synthesize labelType;
//序列化与编码,为存储到本地用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.labelId forKey:@"labelId"];
[aCoder encodeObject:self.labelName forKey:@"labelName"];
[aCoder encodeObject:self.labelType forKey:@"labelType"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
self.labelId = [coder decodeObjectForKey:@"labelId"];
self.labelName = [coder decodeObjectForKey:@"labelName"];
self.labelType = [coder decodeObjectForKey:@"labelType"];
}
return self;
}
@end
即实现 encodeWithCoder 和 initWithCoder 方法。
然后用存储:
NSData *chooseLabelContent =[NSKeyedArchiver archivedDataWithRootObject:keyWord.chooseLabelContent];
[[NSUserDefaults standardUserDefaults] setObject:chooseLabelContent forKey:@"key"];
用如下方法获取到该类:
NSData *dataChooseLabelContent=[[NSUserDefaults standardUserDefaults] objectForKey:@“key”];
labelContentList *chooseLabelContent =[NSKeyedUnarchiver unarchiveObjectWithData:dataChooseLabelContent];
2.计算地图两点之间距离
CLLocation *location1 = [[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1] autorelease];
CLLocation *location2 = [[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] autorelease];
CLLocationDistance distance = [location1 getDistanceFrom:location2];