objective c實現配置檔案 反射

2022-06-01 07:51:09 字數 1105 閱讀 8914

1. 先寫plist配置檔案

plist寫到resouce裡面去

通過nsbundle把資料取plist讀出來

2. 通過nsclassfromstring建立類

nsclassfromstring

nsselectorfromstring

正常來說,

id myobj = [[nsclassfromstring(@"myspecialclass") alloc] init];

id myobj = [[myspecialclass alloc] init];

是一樣的。但是,如果你的程式中並不存在myspecialclass這個類,下面的寫法會出錯,而上面的寫法只是返回乙個空物件而已。

因此,在某些情況下,可以使用nsclassfromstring來進行你不確定的類的初始化。

比如在iphone中,nstask可能就會出現這種情況,所以在你需要使用nstask時,最好使用:

[[nsclassfromstring(@"nstask") .....]]

而不要直接使用[nstask ...]這種寫法。

nsclassfromstring的好處是:

1 弱化連線,因此並不會把沒有的framework也link到程式中。

2 不需要使用import,因為類是動態載入的,只要存在就可以載入。

for (int c=0; c<[classnames count]; c++) {

nsstring *classname=[classnames objectatindex:c];

id class=[[nsclassfromstring(classname) alloc] init];

for (int i=0; i<[params count]; i++) {

[class performselector:nsselectorfromstring([nsstring stringwithformat:@"seta%i",i])];

objective c實現配置檔案 反射 工廠建立

1.先寫plist配置檔案 plist寫到resouce裡面去 通過nsbundle把資料取plist讀出來 2.通過 nsclassfromstring建立類 nsclassfromstring nsselectorfromstring 正常來說,id myobj nsclassfromstrin...

Objective C實現多繼承

我們都知道objective c不能像c 一樣支援多繼承,但是在objective c的使用經常會碰到需要使用多繼承的情況。例如,classa中有methoda,classb中methodb,而現在需要使用這兩個類中的方法。如何按照c 的程式設計思路,毫無疑問採用多繼承就搞定了,在objective...

spring boot mybatis配置檔案開發

之前寫了乙個註解版開發的,現在在乙個配置檔案開發。我直接把 貼下面 根據id查詢單個資訊 public orders getorders integer id 新增單個資訊 mybatis config.xml的配置 insert into orders user id,number,oreatet...