JSONModel:无法将获取的JSON分配给模型

时间:2013-12-10 21:45:58

标签: objective-c jsonmodel

我的JSON我取了:

{"username":"example","confirmed_rewards":"5","round_estimate":"0.73605946","total_hashrate":"0","payout_history":"10","round_shares":"85",

"workers":{
"worker.1":{"alive":"0","hashrate":"0"},
"worker.2":{"alive":"0","hashrate":"0"}
}
}

我的模特:

#import "JSONModel.h"


@protocol LKCoinFCPoolModel @end

@interface LKCoinFCPoolModel : JSONModel

@property (strong, nonatomic) NSString* username;
@property (strong, nonatomic) NSString* confirmed_rewards;
@property (strong, nonatomic) NSString* round_estimate;
@property (strong, nonatomic) NSString* total_hashrate;
@property (strong, nonatomic) NSString* payout_history;
@property (strong, nonatomic) NSString* round_shares;
@property (strong, nonatomic) NSString<Optional> * ErrorCode;

@end

我创建了以下函数,用于获取JSON结构并将其分配给模型。

-(void)viewDidAppear:(BOOL)animated
{

    //show loader view
    [HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];

    //fetch the feed
    LKCoinFCPoolModel* test;
    test = [[LKCoinFCPoolModel alloc] initFromURLWithString:@"http://example.com/ap/key"

                            completion:^(LKCoinFCPoolModel *model, JSONModelError *err) {

                                                                           //hide the loader view
                                                                           [HUD hideUIBlockingIndicator];

                                                                           //json fetched
                                                                           NSLog(@"user: %@", test.username);

                                                                       }];}

我遇到的问题是,user: example代替user: (null)打印{{1}}。

我不确定我做错了什么,这是我尝试用xcode编写的第一个应用程序(我来自Python / Java背景)。

1 个答案:

答案 0 :(得分:0)

您的回调是在LKCoinFCPoolModel *模型中将解析后的JSON传递给您。事实上,我认为你不能按照你的方式进行测试。如果你真的想进入测试模型,你应该在块中分配它。请记住,在下载和解析JSON之后,此块会异步运行。因此,测试在此之前无效。