I am attempting to use this JSONModel library in an app I'm building. I've been following this article, but for some reason I keep getting (null) when attempting to parse the json string. I was told that it is because I'm not creating a json instance; but it seems to me I am. Below is the code, can anyone see what I'm missing? I'm attempting to use this web service, with these inputs:
emailAddress: wufpakjack#yahoo.com;
password: test;
companyId: 2579;
scheduleDate: 20140415.
myShiftModel.h
#import "JSONModel.h"
#interface myShiftModel : JSONModel
#property (assign, nonatomic) NSString *shiftName;
#property (assign, nonatomic) NSString *shiftStartTime;
#property (assign, nonatomic) NSString *shiftPosition;
#end
dailyViewModel.h
#import "JSONModel.h"
#import "myShiftModel.h"
#protocol dailyViewModel #end
#interface dailyViewModel : JSONModel
#property (assign, nonatomic) int numAvailableXChanges;
#property (strong, nonatomic) NSString *todaysForecast;
#property (assign, nonatomic) int todaysTemperature;
#property (strong, nonatomic) myShiftModel *workShift;
#end
dailyFeed.h
#import "JSONModel.h"
#import "dailyViewModel.h"
#interface dailyFeed : JSONModel
#property (strong, nonatomic) NSArray <dailyViewModel> *workShifts;
#end
mainViewController.m
#import "mainViewController.h"
#import "JSONModelLib.h"
#import "JSONModel+networking.h"
#import "dailyFeed.h"
#interface mainViewController () {
dailyFeed *feed;
}
#end
#implementation mainViewController
- (void)viewDidAppear:(BOOL)animated {
NSString *email = [[NSString alloc]initWithFormat:#"wufpakjack#yahoo.com"];
NSString *pass = [[NSString alloc]initWithFormat:#"test"];
NSString *compID = [[NSString alloc]initWithFormat:#"2579"];
NSString *date = [[NSString alloc]initWithFormat:#"20140415"];
NSString *urlString = [NSString stringWithFormat:#"http://qa.shiftzen.com/ws/schedutils.asmx/GetDailyView?emailAddress=%#&password=%#&companyId=%#&scheduleDate=%#", email,pass,compID,date];
NSLog(#"%#", urlString);
[JSONHTTPClient getJSONFromURLWithString:urlString
completion:^(NSDictionary *json, JSONModelError *err) {
NSError* error = nil;
feed = [[dailyFeed alloc] initWithDictionary:json error:&error];
NSLog(#"shifts: %#", feed.workShifts);
}];
}
#end
Any guidance would be greatly appreciated.
Warmest regards,
DB
Related
How can I store array values (RLMArray) in Realm DB ?
My .h file below,
#interface Hotlines : RLMObject
#property (strong, nonatomic) NSString *id;
#property (strong, nonatomic) NSString *department_name;
#property (strong, nonatomic) NSString *flag;
#property (strong,nonatomic) RLMArray<Numbers> *numbers;
#end
You need to create realm model for Numbers array:
#import <Realm/Realm.h>
// Define your models for Numbers array
#interface Numbers : RLMObject
#property NSInteger *num;
#property (strong, nonatomic) NSString *name;
#end
RLM_ARRAY_TYPE(Numbers) // define RLMArray< Numbers >
// Define your models for Numbers array
#interface Hotlines : RLMObject
#property (strong, nonatomic) NSString *id;
#property (strong, nonatomic) NSString *department_name;
#property (strong, nonatomic) NSString *flag;
#property (strong,nonatomic) RLMArray<Numbers> *numbers;
#end
// Implementations
#implementation Numbers
#end // none needed
#implementation Hotlines
#end // none needed
For more information refer Realm Objective c
Update :
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
Hotlines *obj = [[Hotlines alloc] init];
obj.department_name = #"anyString";
Numbers *number = [[Numbers alloc] init]
number.num = 1;
[obj.numbers addObject:number]
[realm addObject:obj];
}];
For multiple data :
[realm transactionWithBlock:^{
Hotlines *obj = [[Hotlines alloc] init];
obj.department_name = #"anyString";
for (int i=0; 1< 10; i++) {
Numbers *number = [[Numbers alloc] init]
number.num = i;
number.name = #"XYZ"
[obj.numbers addObject:number]
}
[realm addObject:obj];
}];
Can be done with primitives if you really just want numbers:
#interface Hotlines : RLMObject
#property NSString *id;
#property NSString *department_name;
#property NSString *flag;
#property RLMArray<NSNumber*><RLMFloat> *numbers;
#end
From the realm docs:
RLMArrays can store primitive values in lieu of Realm objects. In
order to do so, constrain a RLMArray with one of the following
protocols: RLMBool, RLMInt, RLMFloat, RLMDouble, RLMString, RLMData,
or RLMDate
.
I have modeled my api result as following:
#import "PTPDestination.h"
#interface PTPIndex : PTPBaseEntity
#property (strong, nonatomic) NSNumber * citiesCount;
#property (strong, nonatomic) NSNumber * hotelsCount;
#property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations;
#end
I also modeled PTPDestination like this:
#interface PTPDestination : PTPBaseEntity
#property (assign, nonatomic) NSNumber * id;
#property (assign, nonatomic) NSNumber * min_price;
#property (assign, nonatomic) NSNumber * max_discount;
#property (assign, nonatomic) NSString * title;
#end
And I call my api with AFNetworking like this:
AFHTTPSessionManager * manager = [self createAPISessionManager];
[manager GET:[self createServiceUrlWithMethod:#"someURL"] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSError * error = nil;
PTPIndex * index = [[PTPIndex alloc] initWithDictionary:responseObject error:&error];
if (error) {
callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task responseObject:responseObject]);
return;
}
callback (index, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task]);
}];
The problem is with array of destinations. I don't know why the array is not converted to PTPDestination object and it remains as an array of NSDictionaries.
Why this happens and how can I have an array of my custom class?
No, JSON Model Also Converted the Array to JSONObject, if you want to access the PTPDestination class properties.
PTPIndex class
#import "JSONModel.h"
#interface PTPIndex : JSONModel
#property (strong, nonatomic) NSNumber * citiesCount;
#property (strong, nonatomic) NSNumber * hotelsCount;
#property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations;
#end
PPTPDestination class
#import "JSONModel.h"
#interface PTPDestination : JSONModel
#property (assign, nonatomic) NSNumber * id;
#property (assign, nonatomic) NSNumber * min_price;
#property (assign, nonatomic) NSNumber * max_discount;
#property (assign, nonatomic) NSString * title;
#end
NSDictionary "data" from the network Response
PTPIndex *ptpInd = [[PTPIndex alloc] initWithDictionary:data error:&error];
find the total number of PTPDestination and run in the loop.
You can access the object like this.
PTPDestination *ptpdest = [ptpInd PTPDestination[0]];
/*
#protocol ClasConfig
#end
*/
//class A
#interface ClassA : NSObject
#property (nonatomic) *companyA;
#property (nonatomic) NSArray *companyLists;
- (NSMutableArray*)getCompanyLists:(CompanyListModel *)productionListModel;
#end
//class config
#interface ClasConfig : NSObject
#property (nonatomic) NSString *Number;
#property (nonatomic) NSString *totalNumberOfCompany;
#end
#implementation ClassA
- (NSMutableArray*)getCompanyLists:(CompanyListModel *)productionListModel
{
ClasConfig *config = [[ClasConfig alloc] init]; **//Gives me linker error**
}
#end
ClasConfig *config = [[ClasConfig alloc] init]; //Gives me linker error
Though I have tried with importing protocol for reference.
For got to implement class of ClassConfig.
#implementation ClassConfig
#end
#implementation ClassA
- (NSMutableArray*)getCompanyLists:(CompanyListModel *)productionListModel
{
ClasConfig *config = [[ClasConfig alloc] init]; **//Gives me linker error**
}
#end
When I look around the Cocoa Touch API, I can find some classes declared along with categories in the same header file, e.g.
#interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
#property (readonly) NSUInteger count;
// and some other properties
#end
#interface NSArray (NSExtendedArray)
#property (readonly, copy) NSString *description;
// and some other properties
#end
Now I am trying to do the same thing with my class, like as follow:
#interface ARCTextbook : NSObject
#property (nonatomic) NSInteger ID;
#property (nonatomic) NSString *name;
#end
#interface ARCTextbook (Student)
#property (nonatomic) NSInteger studentID;
#property (nonatomic, getter=isUsed) BOOL used; // Used by a student?
#end
However, when I tried to access studentID or used property, I got an unrecognized selector error. Am I missing anything?
Cheers.
This is Associated Objects, you can refer documentation in below:
http://www.davidhamrick.com/2012/02/12/Adding-Properties-to-an-Objective-C-Category.html
How to store not id type variable use a objc_getAssociatedObject/objc_setAssociatedObject?
ARCTextbook.h
#import <Foundation/Foundation.h>
#interface ARCTextbook : NSObject
#property (nonatomic) NSInteger ID;
#property (nonatomic) NSString *name;
#end
#interface ARCTextbook (Student)
#property (nonatomic) NSInteger studentID;
#property (nonatomic, getter=isUsed) BOOL used; // Used by a student?
#end
ARCTextbook.m
#import "ARCTextbook.h"
#import <objc/runtime.h>
#implementation ARCTextbook
#end
static NSString *kStudentID = #"kStudentID";
static NSString *kUsed = #"kUsed";
#implementation ARCTextbook (Student)
#dynamic studentID;
#dynamic used;
- (void)setStudentID:(NSInteger)aStudentID {
objc_setAssociatedObject(self, (__bridge const void *)(kStudentID), #(aStudentID), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSInteger)studentID {
return [objc_getAssociatedObject(self, (__bridge const void *)(kStudentID)) integerValue];
}
- (void)setUsed:(BOOL)aUsed {
objc_setAssociatedObject(self, (__bridge const void *)(kUsed), #(aUsed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isUsed {
return [objc_getAssociatedObject(self, (__bridge const void *)(kUsed)) boolValue];
}
#end
ViewController.m
#import "ViewController.h"
#import "ARCTextbook.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
ARCTextbook *t = [[ARCTextbook alloc] init];
t.studentID = 2;
t.used = YES;
}
#end
i have a problem in reading JSON by JSONModel library https://github.com/icanzilb/JSONModel
and use the KivaDemo at the JSonModel app,the json :
"loans": [
{
"id": 547665,
"name": "Veronica",
"description": {
"languages": [
"en"
]
},
"status": "fundraising",
"funded_amount": 0,
i want to get the "en" ,
#interface KivaFeed : JSONModel
#property (strong, nonatomic) NSArray<LoanModel, ConvertOnDemand>* loans;
#property (strong, nonatomic) Paging *paging;
#end
#protocol LoanModel #end
#interface LoanModel : JSONModel
#property (strong, nonatomic) NSString* name;
#property (strong, nonatomic) NSString* status;
#property (strong, nonatomic) NSString* use;
#property (strong, nonatomic) NSString* id;
#property (strong, nonatomic) NSString* funded_amount;
#property (strong, nonatomic) LocationModel* location;
#property (strong, nonatomic) Image* image;
#property (strong, nonatomic) Description* description;
#end
#interface Description : JSONModel
#property (strong, nonatomic) NSArray<Languages, ConvertOnDemand>* languages;
#end
#protocol Languages #end
#interface Languages : JSONModel
#end
kiva = [[KivaFeed alloc] initFromURLWithString:#"http://api.kivaws.org/v1/loans/search.json?status=fundraising"
completion:^(JSONModel *model, JSONModelError *e) {
[table reloadData];
NSLog(#"kiva.paging.page:%#",kiva.paging.page);
if (e) {
[[NSAlert alertWithError:e] beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:nil contextInfo:nil];
}
[self setLoaderVisible:NO];
}];
LoanModel* loan = kiva.loans[row];
NSString* message = [NSString stringWithFormat:#"%# from %#(%#) needs a loan %#",
loan.name, loan.location.country, loan.location.countryCode, loan.use
];
NSLog(#"loan:%#",loan.id);
NSLog(#"loan:%#",loan.funded_amount);
NSLog(#"loan.image.id:%#",loan.image.id);
NSLog(#"loan.description.languages:%#",loan.description.languages[0]);
last it give me 2013-04-15 13:16:09.163 JSONModelDemo_OSX[2308:303] loan.description.languages:(null). how to get the en,what mistake at my code?
modify as :
#interface Description : JSONModel
#property (strong, nonatomic) NSArray* languages;
#end
all is ok