JSONModel: can't assign fetched JSON to model

135 Views Asked by At

My JSON I fetch:

{"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"}
}
}

My model:

#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

I created the following function which is to fetch a JSON structure and assign it to a model.

-(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);

                                                                       }];}

the problem I'm having is that instead of user: example it prints user: (null).

I'm unsure what I'm doing wrong, it's the first app I'm trying to write in xcode (I'm coming from a Python/Java background).

1

There are 1 best solutions below

0
On

Your callback is passing the the parsed JSON to you in the LKCoinFCPoolModel *model. In fact I don't think that you can assign test the way you do. If you really wanted to get it into the test model, you should assign it inside the block. Remember that this block runs asynchronous after the JSON is downloaded and parsed. So test won't be valid until then.