ios的開發中,小資料量的持久化都用nsuserdefaults來實現,但是nsuserdefaults只能儲存nsstring, nsnumber, nsdate, nsarray, nsdictionary這些資料型別,但大多時候,我們會將乙個物件實體做持久化的儲存,由於不是大批量的資料,不會用到sqlite,那麼這個時候nsuserdefaults會是很好的選擇,其實物件型別可以通過nscoding的委託方法來實現編碼和解碼。
[cpp]view plain
copy
view code
//// userentity.m
// nscodingdemo
//// created by on 11-5-20.
//#import "userentity.h"
#import "addressentity.h"
#define sampledata_key_username (@"username")
#define sampledata_key_gender (@"gender")
#define sampledata_key_age (@"age")
#define k_friends (@"frinds")
#define k_address (@"address")
#define k_userentity_addresses (@"addresses")
@implementation userentity
@synthesize username = _username;
@synthesize gender = _gender;
@synthesize age = _age;
@synthesize friends = _friends;
@synthesize address = _address;
@synthesize addresses = _addresses;
-(void
)dealloc
- (void
)encodewithcoder:(nscoder*)coder
- (id)initwithcoder:(nscoder*)decoder
self.username = [decoder decodeobjectforkey:sampledata_key_username];
self.gender = [decoder decodeboolforkey:sampledata_key_gender];
self.age = [decoder decodeintforkey:sampledata_key_age];
self.friends = [decoder decodeobjectforkey:k_friends];
self.address = [decoder decodeobjectforkey:k_address];
self.addresses = [decoder decodeobjectforkey: k_userentity_addresses];
} return
self;
}
@end
[cpp]view plain
copy
view code
//// addressentity.m
// nscodingdemo
//// created by on 11-5-20.
//#import "addressentity.h"
#define k_id (@"id")
#define k_addresentity_address (@"addrss")
@implementation addressentity
@synthesize id, address;
-(void
)dealloc
- (nsstring *)description
- (void
)encodewithcoder:(nscoder*)coder
- (id)initwithcoder:(nscoder*)decoder
self.address = [decoder decodeobjectforkey:k_addresentity_address];
self.id = [decoder decodeintforkey:k_id];
} return
self;
}
@end
[cpp]view plain
copy
view code -(ibaction)write:(id)sender
-(ibaction)read:(id)sender
LeetCode 實現 歸檔一
編號 題名題解 難度更新狀態 1兩數之和 雜湊表簡單 2 兩數相加 鍊錶中等 3 無重複字元的最長子串 雜湊表中等 4 尋找兩個有序陣列的中位數 二分查詢困難5 最長回文子串 動態規劃中等 6 z 字形變換 字串中等 7 整數反轉 數學簡單 8 字串轉換整數 字串中等 9 回文數數學簡單 10正規表...
歸檔以及反歸檔
歸檔和反歸檔 複雜的物件我們並不能通過writetofile型別的方法寫入到檔案中。這裡的複雜物件指的是在foundation框架內部存在的資料類,這個負載物件至少包含有乙個例項物件 如果想要進行歸檔和反歸檔操作,則必須遵守 協議 我們在歸檔和解檔操作時,每乙個需要乙個鍵.並且歸檔時是什麼鍵,那麼解...
Swift 歸檔解歸檔
swift中物件進行歸檔寫入檔案時需要對物件屬性進行歸檔操和解歸檔操作 物件方法中需重寫以下兩個方法 從nsobject解析回來 init coder adecoder nscoder 編碼成object func encodewithcoder acoder nscoder 歸檔,解歸檔需要使用到...