Runtime實現歸 解檔

2021-09-11 12:24:35 字數 1780 閱讀 7074

#import 

@inte***ce nsobject (agcoding) // 遵守編碼協議

@end

複製**

#import "nsobject+agcoding.h"

#import

#import

/* 成員變數型別

獲取 例項變數的型別 方法

nslog(@"%s", ivar_gettypeencoding(array[i]));

*/static nsstring *inttype = @"i"; // int

static nsstring *integertype = @"q"; // long

static nsstring *floattype = @"f"; // float

static nsstring *doubletype = @"d"; // double

static nsstring *booltype = @"b"; // bool

static nsstring *imagetype = @"uiimage"; // uiimage 型別

static nsstring *stringtype = @"nsstring"; // nsstring 型別

// 定義屬性字典,用來儲存 屬性名(key) 型別(value)

// 比如: age q

static nsmutabledictionary *prodic = nil;

@implementation nsobject (agcoding)

// 歸檔是乙個編碼的過程

- (void)encodewithcoder:(nscoder *)acoder

// (4). prodic字典賦值 : 屬性名(key)_屬性型別(value)

if (![proname isequaltostring:@"prodic"])

// (5). 根據型別進行編碼

if ([protype isequaltostring:integertype]) else if ([protype isequaltostring:imagetype]) else if ([protype isequaltostring:stringtype])

// 若再有型別新增即可

} // for 迴圈,獲取結束

// 釋放vararray

free(vararray);

}// 反歸檔,是乙個解碼的過程。

- (instancetype)initwithcoder:(nscoder *)adecoder

else if ([protype isequaltostring:imagetype]) else if ([protype isequaltostring:stringtype])

// 若再有型別新增即可

} // for 迴圈結束

}return self;

}// 解碼時重新給當前類的prodic賦值

- (void)setprodic

// (4). prodic字典賦值 : 屬性名(key)_屬性型別(value)

if (![proname isequaltostring:@"prodic"])

} // for 迴圈,獲取結束

// 釋放vararray

free(vararray);

}@end

複製**

Runtime實現歸 解檔

import inte ce nsobject agcoding 遵守編碼協議 end 複製 import nsobject agcoding.h import import 成員變數型別 獲取 例項變數的型別 方法 nslog s ivar gettypeencoding array i stat...

利用runtime進行歸檔解檔

做過ios開發的應該都知道資料的本地化的方式,其中歸檔就是其中的一種。說實話,在本人在專案中並不是很常用歸檔來進行資料的本地儲存。今天之所以寫這篇部落格是因為最近了解到原來我們還能利用runtime進行歸檔和解檔。先來看一下我們之前的歸檔 解檔 例如我們要對person這個擁有name和age屬性的...

iOS 物件的歸檔 解檔 runtime

ios 物件的歸檔 解檔 runtime 若要例項物件實現歸檔解檔,需要該類遵守nscoding協議,及以下協議方法 專案中以ycarchivebase類為例,可直接新增屬性使用 歸檔和接檔的操作以類方法實現如下 可自己建立管理類分離出去 archivefilepath 為檔案儲存路徑 void a...