大家好,我在 * OBJECTIVE C * 方面需要幫助 * 我是 Objc 的新手——但不是很快。但我發現自己在簡單的 json 決議和構建模型上苦苦掙扎。
這是我要決議的模型的示例。
{
"success": true,
"data": {
"memes": [
{
"id": "61579",
"name": "One Does Not Simply",
"url": "https://i.imgflip.com/1bij.jpg",
"width": 568,
"height": 335,
"box_count": 2
},
{
"id": "101470",
"name": "Ancient Aliens",
"url": "https://i.imgflip.com/26am.jpg",
"width": 500,
"height": 437,
"box_count": 2
}
// probably a lot more memes here..
]
}
}
我想為這個 json 物件創建一個“MemeModel”。并在我的 viewController 上決議它,然后添加到我的 dataSource 陣列中......誰能解釋我或告訴我應該如何完成......所有舊的 objc 谷歌搜索都沒有幫助我。謝謝你!
QTMeme.h 檔案
@interface QTMeme : NSObject
@property (nonatomic,strong) QTData *data;
@end
@interface QTData : NSObject
@property (nonatomic,copy) NSMutableArray<QTMemeResponse *> *memes;
@end
@interface QTMemeResponse : NSObject
@property (nonatomic,copy) NSString *url;
@property (nonatomic,copy) NSString *name;
-(instancetype) initMemesWithImage:(NSString *)url withTitle:(NSString *)name;
@end
這是 QTMeme.m 檔案..
@implementation QTMeme
@end
@implementation QTData
@end
@implementation QTMemeResponse
-(instancetype) initMemesWithImage:(NSString *)url withTitle:(NSString *)name {
self = [super init];
if (self) {
self.url = url;
self.name = name;
}
return self;
}
@end
uj5u.com熱心網友回復:
首先,讓我們重命名您的類,它們具有誤導性:
QTMeme -> QTMemeResponse
QTData -> QTMemeResponseData
QTMemeResponse -> QTMeme
現在,由于我們解碼物件字典,讓我們initWithJSONDictionary:為每個物件添加一個方法。
@interface QTMeme : NSObject
@property (nonatomic,copy) NSString *url;
@property (nonatomic,copy) NSString *name;
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict;
-(instancetype) initMemesWithImage:(NSString *)url withTitle:(NSString *)name;
@end
@interface QTMemeResponseData : NSObject
@property (nonatomic,copy) NSMutableArray<QTMeme *> *memes;
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict;
@end
@interface QTMemeResponse : NSObject
@property (nonatomic,strong) QTMemeResponseData *data;
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict;
@end
執行:
@implementation QTMemeResponse
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict {
self = [super init];
if (self) {
self.data = [[QTMemeResponseData alloc] initWithJSONDictionary:dict[@"data"]];
}
return self;
}
@end
@implementation QTMemeResponseData
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict {
self = [super init];
if (self) {
NSArray *jsonDicts = dict[@"memes"];
_memes = [[NSMutableArray alloc] init];
for (NSDictionary *aJSONDict in jsonDicts) {
QTMeme *aMeme = [[QTMeme alloc] initWithJSONDictionary:aJSONDict];
[_memes addObject:aMeme];
}
}
return self;
}
@end
@implementation QTMeme
-(instancetype)initWithJSONDictionary:(NSDictionary *)dict {
return [self initMemesWithImage:dict[@"name"] withTitle:dict[@"url"]];
}
-(instancetype) initMemesWithImage:(NSString *)url withTitle:(NSString *)name {
self = [super init];
if (self) {
self.url = url;
self.name = name;
}
return self;
}
@end
解碼:
NSError *decodingError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&decodingError];
if (decodingError) {
NSLog(@"Error while decoding JSON: %@ with stringified data: %@", decodingError, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
} else {
QTMemeResponse *decoded = [[QTMemeResponse alloc] initWithJSONDictionary:json];
NSLog(@"decoded: %@", decoded);
}
輸出:
$>decoded: <QTMemeResponse: 0x600001764810>
您可以在除錯器中看到這些值,但也許讓我們通過覆寫添加一些有用的日志description:
對于QTMemeResponse:
-(NSString *)description {
return [NSString stringWithFormat:@"%@ data: %@", [super description], self.data];
}
對于QTMemeResponseData:
-(NSString *)description {
return [NSString stringWithFormat:@"%@ memes: %@", [super description], self.memes];
}
對于QTMeme:
-(NSString *)description {
return [NSString stringWithFormat:@"%@ name: %@, url: %@", [super description], self.name, self.url];
}
現在輸出:
$>decoded: <QTMemeResponse: 0x600001764810> data: <QTMemeResponseData: 0x6000017648b0> memes: (
"<QTMeme: 0x6000015300e0> name: https://i.imgflip.com/1bij.jpg, url: One Does Not Simply",
"<QTMeme: 0x600001530100> name: https://i.imgflip.com/26am.jpg, url: Ancient Aliens"
)
請注意,在Codable使用(NS)JSONSerialization. 當然, for 回圈可能是 a compactMap(),您需要做很多as? [String: Any],等等,但這是相同的想法......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/488725.html
