使用 JSONModel 將網路資料映射為模型時,外層模型包含一個內層模型陣列。按照文檔在屬性宣告中使用 <InnerModel> 卻報錯,必須加上 * 才能編譯通過。
<> 語法包裹的是 Protocol (協議),而非具體類別。必須確認內層模型 DataModel.h 中已使用 @protocol 關鍵字宣告協議,並讓該類別遵循此協議。
// DataModel.h
@protocol DataModel <JSONModel>
@property (nonatomic, strong) NSString *name;
// ... 其他屬性
@end
@interface DataModel : JSONModel <DataModel>
@end
// OuterModel.h
@protocol OuterModel <JSONModel>
@property (nonatomic, strong) NSArray<DataModel> *items; // 使用協議名
@end
NSArray<ProtocolName> *items 或 NSArray *items 搭配 @property (nonatomic, strong) NSArray<DataModel> *items; (需導入頭文件)。