1

私がフェッチする私の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)です。

これは、xcode で記述しようとしている最初のアプリです (私は Python/Java のバックグラウンドから来ています)。

4

1 に答える 1

0

あなたのコールバックは、解析された JSON を LKCoinFCPoolModel * モデルであなたに渡しています。実際、あなたのようにテストを割り当てることはできないと思います。本当にテスト モデルに入れたい場合は、ブロック内に割り当てる必要があります。このブロックは、JSON がダウンロードされて解析された後に非同期で実行されることに注意してください。そのため、テストはそれまで有効ではありません。

于 2013-12-10T21:57:10.833 に答える