JSON to Object in iOS with objective-c - ios

I have object
#interface Hourly (CoreDataProperties)
#property (nullable, nonatomic, retain) NSDate *dt;
#property (nullable, nonatomic, retain) NSNumber *temp;
#property (nullable, nonatomic, retain) NSNumber *temp_min;
#property (nullable, nonatomic, retain) NSNumber *temp_max;
#property (nullable, nonatomic, retain) NSNumber *humidity;
#property (nullable, nonatomic, retain) NSNumber *pressure;
#property (nullable, nonatomic, retain) NSString *main;
#property (nullable, nonatomic, retain) NSString *des;
#property (nullable, nonatomic, retain) NSString *icon;
#end
And url
http://api.openweathermap.org/data/2.5/forecast/dayly?lat=35&lon=139&cnt=2
I can' add json to object by jsonmodel pod because weather in list is a array .

#Ngọc Thái:
I don't really understand your question. But, if you want parse from JSON (contain array of objects) to object.
Simple, you can try it: https://github.com/elado/jastor#arrays
Hope this help!

Try my project BWJSONMatcher. It helps you easily convert a json string to NSArray:
NSArray *dataArray = [BWJSONMatcher matchJSON:YOURJSONSTRING withClass:[Hourly class]];
The library is fully opensourced and under MIT license.

Related

Realm Error: Property requires a protocol defining the contained type

I have the following model and I'm using Realm:
#interface GUIRoutineModel : GUIModel # GUIModel is a subclass of RLMObject
#property (nonatomic, retain) NSString *dateCreated;
#property (nonatomic, retain) NSString *dateModified;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *type;
#property NSInteger userId;
#property int routineId; #also have same issue using NSInteger
#end
When I call:
// Persist to Realm DB
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:routineModel];
}];
I get the following error:
'Property 'routineId' requires a protocol defining the contained type - example: NSNumber<RLMInt>.'
I have tried changing the routineId property to NSNumber<RLMint>, but that didn't work either. Can anyone tell me what I'm doing wrong?
UPDATE:
Here is another version of the model that I have tried:
#interface GUIRoutineModel : GUIModel
#property (nonatomic, retain) NSString *dateCreated;
#property (nonatomic, retain) NSString *dateModified;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *type;
#property NSInteger userId;
#property NSNumber<RLMInt> *routineId;
#end
The Property requires a protocol defining the contained type error is only generated by Realm for a property of type NSNumber without a protocol annotating the expected concrete type. That means it cannot be generated for either of the model classes you mention. It's likely you have a another routineId property elsewhere in your app that is triggering the error.

How to sort SKProduct by Apple ID

I'd like to sort those products retrieved from itunesconnect.apple.com by AppleID that are online generated automatically.
I checked SKProduct, but only find these propertis, none of which are referred to Apple ID,
#property(nonatomic, readonly) NSString *localizedDescription
#property(nonatomic, readonly) NSString *localizedTitle
#property(nonatomic, readonly) NSDecimalNumber *price
#property(nonatomic, readonly) NSLocale *priceLocale
#property(nonatomic, readonly) NSString *productIdentifier
#property(nonatomic, readonly, getter=isDownloadable) BOOL downloadable
#property(nonatomic, readonly) NSArray *downloadContentLengths
#property(nonatomic, readonly) NSString *downloadContentVersion
Does anyone know about it?

parse json using JsonModel

Facing problem while getting a array of json.
I am using JsonModel library for json handelling.
My json data is:
[
{
"TSCard_id": 100,
"TSCardBackend_id": "TSu1t1",
"TSCard_date": "10/11/2013",
"TSCard_resource_id": "",
"TSCard_resource_name": "",
"TSCard_job_id": "JOB_M2156 ",
"TSCard_job_desc": "BAGARIA MILLIPORE - BOM ",
"TSCard_activity_id": "B03",
"TSCard_activity_desc": "Closing",
"TSCard_hours": "4.0",
"TSCard_chargeableflag": "0",
"TSCard_desc": "Description:",
"TSCard_syncflag": "0",
"TSCard_flag": "",
"user_id": "1"
},
{
"TSCard_id": 101,
"TSCardBackend_id": "TSu1t2",
"TSCard_date": "12/11/2013",
"TSCard_resource_id": "",
"TSCard_resource_name": "",
"TSCard_job_id": "JOB_B0002",
"TSCard_job_desc": "ABB LIMITED - BLR ",
"TSCard_activity_id": "NB01",
"TSCard_activity_desc": "Admin ",
"TSCard_hours": "5.0",
"TSCard_chargeableflag": "1",
"TSCard_desc": "Description:",
"TSCard_syncflag": "0",
"TSCard_flag": "",
"user_id": "1"
}
]
Above json has 2 objects of type TSCard class
//TSCard.h
#import <Foundation/Foundation.h>
#import <JSONModel.h>
#protocol TSCard #end
#interface TSCard : JSONModel
#property (nonatomic, retain) NSString *TSCard_id;
#property (nonatomic, retain) NSString *TSCardBackend_id;
#property (nonatomic, retain) NSString *TSCard_date;
#property (nonatomic, retain) NSString *TSCard_resource_id;
#property (nonatomic, retain) NSString *TSCard_resource_name;
#property (nonatomic, retain) NSString *TSCard_job_id;
#property (nonatomic, retain) NSString *TSCard_job_desc;
#property (nonatomic, retain) NSString *TSCard_activity_id;
#property (nonatomic, retain) NSString *TSCard_activity_desc;
#property (nonatomic, retain) NSString *TSCard_hours;
#property (nonatomic, retain) NSString *TSCard_chargeableflag;
#property (nonatomic, retain) NSString *TSCard_desc;
#property (nonatomic, retain) NSString *TSCard_syncflag;
#property (nonatomic, retain) NSString *TSCard_flag;
#property (nonatomic, retain) NSString *user_id;
#end
And I am making a class for array of type TSCard
//GetCardData.h
#import <JSONModel.h>
#import "TSCard.h"
#interface GetCardData : JSONModel
#property(strong,nonatomic)NSArray<TSCard>* myDataArray;
#end
then I parsing json as below:
NSError* err = nil;
GetCardData *c = [[GetCardData alloc] initWithString:jsonString error:&err];
NSLog(#"%d",c.myDataArray.count);
NSLog Error:
Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'." UserInfo=0x8265490 {NSLocalizedDescription=Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'.}
I can't find where I am wrong...can anybody suggest the right way of using JsonModel and parsing a jsonString to array of TSCard objects correctly.
Try using following code for parsing your json and getting array
NSMutableArray *yourArray = [TSCard arrayOfModelsFromDictionaries:jsonString];
Than create and assign
GetCardData *objCardData = [[GetCardData alloc] init];
objCardData.myDataArray = yourArray;
or you need to change your JSONString as follow
{
"myDataArray": [
{
"TSCard_id": 100,
"TSCardBackend_id": "TSu1t1",
"TSCard_date": "10/11/2013",
"TSCard_resource_id": "",
"TSCard_resource_name": "",
"TSCard_job_id": "JOB_M2156 ",
"TSCard_job_desc": "BAGARIA MILLIPORE - BOM ",
"TSCard_activity_id": "B03",
"TSCard_activity_desc": "Closing",
"TSCard_hours": "4.0",
"TSCard_chargeableflag": "0",
"TSCard_desc": "Description:",
"TSCard_syncflag": "0",
"TSCard_flag": "",
"user_id": "1"
},
{
"TSCard_id": 101,
"TSCardBackend_id": "TSu1t2",
"TSCard_date": "12/11/2013",
"TSCard_resource_id": "",
"TSCard_resource_name": "",
"TSCard_job_id": "JOB_B0002",
"TSCard_job_desc": "ABB LIMITED - BLR ",
"TSCard_activity_id": "NB01",
"TSCard_activity_desc": "Admin ",
"TSCard_hours": "5.0",
"TSCard_chargeableflag": "1",
"TSCard_desc": "Description:",
"TSCard_syncflag": "0",
"TSCard_flag": "",
"user_id": "1"
}
]
}
Than your code will definitely work. Tell me if need more help.
For your problem, I personally recommend BWJSONMatcher. You don't have to do anything else or declare any protocol apart from your data model:
#interface TSCard : NSObject
#property (nonatomic, strong) NSString *TSCard_id;
#property (nonatomic, strong) NSString *TSCardBackend_id;
#property (nonatomic, strong) NSString *TSCard_date;
#property (nonatomic, strong) NSString *TSCard_resource_id;
#property (nonatomic, strong) NSString *TSCard_resource_name;
#property (nonatomic, strong) NSString *TSCard_job_id;
#property (nonatomic, strong) NSString *TSCard_job_desc;
#property (nonatomic, strong) NSString *TSCard_activity_id;
#property (nonatomic, strong) NSString *TSCard_activity_desc;
#property (nonatomic, strong) NSString *TSCard_hours;
#property (nonatomic, strong) NSString *TSCard_chargeableflag;
#property (nonatomic, strong) NSString *TSCard_desc;
#property (nonatomic, strong) NSString *TSCard_syncflag;
#property (nonatomic, strong) NSString *TSCard_flag;
#property (nonatomic, strong) NSString *user_id;
#end
You can then get your array with one neatly short line:
NSArray *dataArray = [BWJSONMatcher matchJSON:jsonString withClass:[TSCard class]];
You can using this method in JSONModel
NSError *error;
NSMutableArray <TSCard *> *yourArray = [TSCard arrayOfModelsFromString:strData error:&error];
and do your logic.

Attribute of relationship in Core Data is returning nil when being called in iOS

I have an iOS application where I am using Core Data for storage. I have two entities ("MyEntity", and "OtherEntity") which are in a one to one relationship. Both entities are related to each other (i.e. each entity has an inverse relationship with the other). In addition to having a relationship with each other, one of the attributes of each entity is also the primarykey of the other entity.
The problem I am having is that I realize that I shouldn't have an attribute that is the foreign key of another entity, when the entities have a relationship with each other, however I am unable to retrieve the primary key indirectly by referencing the relationship attribute, but I am able to retrieve it by referencing the attribute that I explicitly set to the primary key of the other entity:
//NSInteger userId = [testUser.otherEntity.userId integerValue]; --> returns nil
NSInteger userId = [testUser.userId integerValue]; --> works fine
where "testUser" is an instance of type "MyEntity" which is a subclass of NSManagedObject, and "otherEntity" is an instance of the Entity, "OtherEntity" that has an inverse relationship with "MyEntity".
Here are the attributes of each Entity:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class OtherEntity;
#interface MyEntity : NSManagedObject
#property (nonatomic, strong) NSString * email;
#property (nonatomic, strong) NSNumber * primaryId; //primary key for MyEntity
#property (nonatomic, strong) NSString * metaData;
#property (nonatomic, strong) NSDate * birthDate;
#property (nonatomic, strong) NSString * name;
#property (nonatomic, strong) NSNumber * userId; //this is the primary key for OtherEntity
#property (nonatomic, strong) OtherEntity *otherEntity;
#end
and here are the attributes for OtherEntity:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class MyEntity;
#interface OtherEntity : NSManagedObject
#property (nonatomic, strong) NSString * address;
#property (nonatomic, strong) NSString * city;
#property (nonatomic, strong) NSString * country;
#property (nonatomic, strong) NSString * fax;
#property (nonatomic, strong) NSString * phone;
#property (nonatomic, strong) NSString * postalCode;
#property (nonatomic, strong) NSString * province;
#property (nonatomic, strong) NSNumber * myUserId//primary key of MyEntity
#property (nonatomic, strong) NSNumber * userId;//primary key of Other Entity
#property (nonatomic, strong) MyEntity *myEntity;
#end
What is it that I'm doing wrong?
(From the comments:) You did not set a relationship for testUser,
Therefore testUser.otherEntity is nil.
Then testUser.otherEntity.userId is also nil and [testUser.otherEntity.userId integerValue] return zero.

ios : how to insert null value into sqlite with fmdb

my sqlite table structure is :
CREATE TABLE "location" ("latitude" DOUBLE,"longitude" DOUBLE,"date" DATETIME,"locationDescription" VARCHAR,"photopath" VARCHAR,"placemark" BLOB,"wheelId" INTEGER,"wheelSize" DOUBLE,"frontWheelx" DOUBLE,"frontWheely" DOUBLE,"rearWheelx" DOUBLE,"rearWheely" DOUBLE)
and now I want to insert one record into the table, insert column : ( date , photopath , wheelId , wheelSize , frontWheelx , frontWheely , rearWheelx , rearWheely ) and ignore column : (latitude , longitude , locationDescription , placemark)
my code is :
BOOL succeed = [lunGaiDataBase executeUpdate:#"insert into location (date, photopath, wheelid, wheelsize, frontwheelx, frontwheely, rearwheelx, rearwheely) values(?,?,?,?,?,?,?,?)",
location.date,location.photoPath, location.wheelId,location.wheelSize,location.frontWheelx,location.frontWheely,location.rearWheelx,location.rearWheely];
my location class is :
#property (nonatomic, assign) double latitude;
#property (nonatomic, assign) double longitude;
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSString * locationDescription;
#property (nonatomic, retain) NSString * photoPath;
//#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) CLPlacemark * placemark;
#property (nonatomic, assign) NSInteger wheelId;
#property (nonatomic, assign) double frontWheelx;
#property (nonatomic, assign) double frontWheely;
#property (nonatomic, assign) double rearWheelx;
#property (nonatomic, assign) double rearWheely;
#property (nonatomic, assign) double wheelSize;
when I run the program, it crashed !! Help me !!
If i've not mistaken, your crash message would be something like "EXC_BAD_EXCESS" right?
Try this :
BOOL succeed = [lunGaiDataBase executeUpdate:#"insert into location (wheelId) values(?)",
[NSNumber numberWithInt:location.wheelId]];
Since wheelId is declared as NSInteger, you've got to pass it as
[NSNumber numberWithInt:location.wheelId]

Resources