今天在這邊記錄一下數(shù)據(jù)庫(kù)處理NSArray存儲(chǔ)的問題凸郑!大家都知道Sqlite是不支持以數(shù)組的格式直接進(jìn)行存儲(chǔ)的矛市!所以對(duì)于處理數(shù)組的時(shí)候大家都是先將其轉(zhuǎn)換為NSData的模式,然后調(diào)用的時(shí)候在轉(zhuǎn)換回來。
- 存儲(chǔ)轉(zhuǎn)換:
NSArray * arr = @[@"First", @"Second"];
NSMutableData * mutableData = [[NSMutableData alloc] init]; //可變的data
for (NSString * str in arr) {
NSString * strData = [str stringByAppendingString:@"-"];//添加間隔救氯,為了區(qū)分?jǐn)?shù)組的每個(gè)元素
NSData *temp =[strData dataUsingEncoding:NSASCIIStringEncoding]; //NSString -> NSData
[mutableData appendData:temp];//data數(shù)據(jù)拼接到最終數(shù)據(jù)mutableData上
}
以上就是將一個(gè)數(shù)組轉(zhuǎn)換為MutableData歌憨,這樣就可以進(jìn)行數(shù)據(jù)庫(kù)存儲(chǔ)操作了着憨!
取出來的時(shí)候?qū)⑵滢D(zhuǎn)換回來也是很簡(jiǎn)單的事情:
NSString * string01 = [[NSString alloc]initWithData:mutableData encoding:NSUTF8StringEncoding];//NSData -> NSString
NSArray * array02 = [string01 componentsSeparatedByString:@"-"];//拆分
之前我是一直這樣操作的务嫡,我相信還有很多人是這樣解決的!
最近在寫swift的時(shí)候無意間遇到了NSCoding(下文會(huì)詳細(xì)介紹)准谚,在看相關(guān)文檔的時(shí)候,我看到了更簡(jiǎn)單的方法柱衔。
- 首先是可以歸檔解檔解決字符串?dāng)?shù)組的存儲(chǔ)問題
//歸檔
NSData *data02 = [NSKeyedArchiver archivedDataWithRootObject:arr];//對(duì)數(shù)組進(jìn)行歸檔愉棱,然后可以進(jìn)行存儲(chǔ)操作
//解檔
NSArray *array03 = [NSKeyedUnarchiver unarchiveObjectWithData:data02];
一句代碼解決問題,現(xiàn)在看看之前的方法經(jīng)過那么多次的轉(zhuǎn)換奔滑,真是麻煩!
那么我們?cè)谑裁磿r(shí)候可以用到NSCoding呢朋其,上文中我們用的數(shù)組中的元素都是字符串,那么如果是類呢叼耙?大家可以自己嘗試一下,結(jié)果肯定是不可行的筛婉。所有原生的類都要實(shí)現(xiàn)NSCoding協(xié)議癞松,在歸檔的過程中會(huì)實(shí)現(xiàn)轉(zhuǎn)碼,這樣才能歸檔成功
- API
//Encodes the receiver using a given archiver. (required)
// decoder An unarchiver object.
- (void)encodeWithCoder:(NSCoder *)encoder
//Returns an object initialized from data in a given unarchiver.(required)
// decoder An unarchiver object.
- (id)initWithCoder:(NSCoder *)decoder
NSCoding協(xié)議中只有兩個(gè)方法响蓉,都是require的方法,一個(gè)是把本身的類進(jìn)行轉(zhuǎn)碼枫甲,一個(gè)是將轉(zhuǎn)換后的碼轉(zhuǎn)成類對(duì)象扼褪。下面我們寫一個(gè)類看看是否可行粱栖,首先寫一個(gè)自定義Animal類:
@interface Animal : NSObject<NSCoding>
@property (nonatomic, retain) NSString *animalKind;
@property (nonatomic, retain) NSString *animalWeight;
-(Animal *)initWithKind:(NSString*) animalKind
andWeight:(NSString *) animalWeight;
@end
在.m文件中實(shí)現(xiàn)NSCoding協(xié)議
@implementation Animal
@synthesize animalKind = _animalKind, animalWeight = _animalWeight;
//初始化
-(Animal *)initWithKind:(NSString*) animalKind
andWeight:(NSString *) animalWeight{
self = [super init];
if (self) {
self.animalKind = animalKind;
self.animalWeight = animalWeight;
}
return self;
}
//兩個(gè)屬性變量分別轉(zhuǎn)碼
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.animalKind forKey:@"animalKind"];
[aCoder encodeObject:self.animalWeight forKey:@"animalWeight"];
}
//分別把兩個(gè)屬性變量根據(jù)關(guān)鍵字進(jìn)行逆轉(zhuǎn)碼,最后返回一個(gè)對(duì)象
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.animalKind = [aDecoder decodeObjectForKey:@"animalKind"];
self.animalWeight = [aDecoder decodeObjectForKey:@"animalWeight"];
}
return self;
}
@end
自定義Animal歸檔轉(zhuǎn)換的具體實(shí)現(xiàn):
Animal *dog = [[Animal alloc]initWithKind:@"Dog" andWeight:@"30K"];
Animal *cat = [[Animal alloc]initWithKind:@"Cat" andWeight:@"12K"];
NSArray *animalArray =[NSArray arrayWithObjects:dog, cat, nil];
NSData *animalData = [NSKeyedArchiver archivedDataWithRootObject: animalArray];//歸檔
NSArray *animalArray2 =[NSKeyedUnarchiver unarchiveObjectWithData: animalData];//解檔
大功告成(swift同樣可行)