国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

Objective-C基礎 自定義對象歸檔詳解及簡單實例

2019-10-21 18:47:12
字體:
來源:轉載
供稿:網友

自定義對象要實現歸檔必須實現NSCoding協議

 NSCoding協議有兩個方法,encodeWithCoder方法對對象的屬性數據做編碼處理,initWithCoder解碼歸檔數據來初始化對象。

示例1

.h頭文件

 #import <Foundation/Foundation.h> @interface user : NSObject <NSCoding> @property(nonatomic,retain)NSString *name; @property(nonatomic,retain)NSString *email; @property(nonatomic,retain)NSString *pwd; @property(nonatomic,assign)int age; @end

.m實現文件

#import "user.h"#define AGE @"age"#define NAME @"name"#define EMAIL @"email"#define PASSWORD @"password"@implementation user//對屬性編碼- (void)encodeWithCoder:(NSCoder *)aCoder{  [aCoder encodeInt:_age forKey:@"age"];  [aCoder encodeObject:_name forKey:AGE];  [aCoder encodeObject:_email forKey:EMAIL];  [aCoder encodeObject:_pwd forKey:PASSWORD];}//對屬性解碼- (id)initWithCoder:(NSCoder *)aDecoder{  self=[super init];  if(self)  {    self.age=[aDecoderdecodeIntForKey:AGE];    self.name=[aDecoderdecodeObjectForKey:NAME];    self.email=[aDecoderdecodeObjectForKey:EMAIL];    self.pwd=[aDecoderdecodeObjectForKey:PASSWORD];  }  return self;}-(void)dealloc{  [_name release];  [_email release];  [_pwd release];  [super dealloc];}@end

main函數的調用

  user *userObj=[[user alloc] init];  userObj.age=33;  userObj.email=@"adfdadf@qq.com";  userObj.pwd=@"212212";  userObj.name=@"ricard";  NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/custom.text"];  BOOL succ=[NSKeyedArchiver archiveRootObject:userObj toFile:path];  if (succ) {     NSLog(@"Hello, World!");     user *usertemp=[NSKeyedUnarchiver unarchiveObjectWithFile:path];  }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

 

注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙陵县| 赤壁市| 南阳市| 寿阳县| 沐川县| 望谟县| 黄龙县| 安塞县| 大新县| 上高县| 房山区| 通渭县| 莱阳市| 梧州市| 门源| 福海县| 泉州市| 志丹县| 娱乐| 中山市| 都匀市| 台东市| 章丘市| 东阿县| 朝阳县| 唐山市| 隆林| 正镶白旗| 顺平县| 饶河县| 和静县| 永丰县| 大洼县| 珠海市| 花莲县| 苗栗县| 伊金霍洛旗| 织金县| 大城县| 中方县| 滨海县|