{ "lastUpdated":0, "firstItemParent":{ "title":"Item 1 title", "text":"Item 1 text", "firstItemList":[ { "title":"Kitchen" }, { "title":"Lounge" }, { "title":"Bathroom" }, { "title":"Garage" } ] } }
need to write RestKit mapping for this data received. Can anybody help me with this.
I tried making firstItemParent element as a class and tried firstItemList as a path nothing worked, and i am new to restkit mapping.
To begin with first you need to create mapping model classes, as per the JSON the mapping classes and properties shown below.
Class ResponseModel
#property (assign,nonatomic) int lastUpdated;
#property (strong, nonatomic) FirstItemParent *firstItemParent;
Class FirstItemParent
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) NSString *text;
#property (strong, nonatomic) NSMutableArray *firstItemList;
Class FirstItemList
#property (strong, nonatomic) NSString *title;
After creating the modal classes you have to write the relation mappings to it.
Related
I am new to objective c. I have an mainViewController class and i created another object class. I am declaring the other class inside the .h file of the mainViewController. I use
#import <Foundation/Foundation.h>
#import "OtherClass.h"
NS_ASSUME_NONNULL_BEGIN
#interface ViewController : UIViewController
#property (strong, nonatomic) OtherClass *jsonData;
#end
NS_ASSUME_NONNULL_END
Then i call a function of the Other Class from the mainViewController .m file.
The method returns NSMutableArray and it works. But there is a possibility an error might occur so it might not return the NSMutableArray but an NSError. I need the class to return the error to the mainViewController.
How can i achieve this? Any help appreciated.
You can use: id jsonData instead of OtherClass *jsonData:
#property (strong, nonatomic) OtherClass *jsonData;
=>
#property (strong, nonatomic) id jsonData;
And then do
if ([jsonData isKindOfClass: [OtherClass class]])
{
}
else if ([jsonData isKindOfClass: [NSError class]])
{
}
else
{
//It's none
}
But maybe you want to have 2 properties, might be simpler to use with just if (jsonData) {} or if (jsonError) {}?
#property (strong, nonatomic) OtherClass *jsonData;
#property (strong, nonatomic) NSError *jsonError;
Another possibility would be to embed that into a custom object:
#interface ResponseData: NSObject
#property (strong, nonatomic) OtherClass *data;
#property (strong, nonatomic) NSError *error;
#end
And then:
#interface ViewController : UIViewController
#property (strong, nonatomic) ResponseData *json;
#end
And check json.data or json.error?
My Objective-C is a little rusty, but I believe I can help.
id in ObjC is quite similar to AnyObject in Swift.
So your type could use id but then you would need to cast it
You could us the isKindOfClass to test which type is returned and then cast it.
#property (strong, nonatomic) id *someType;
if [someType isKindOfClass:[NSMutableArray class]) {
}
if [someType isKindOfClass:[NSError class]) {
}
if Data is
{ "id": "10", "country": "Germany", "dialCode": 49, "isInEurope": true }
someone using
#interface CountryModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* country;
#property (strong, nonatomic) NSString* dialCode;
#property (assign, nonatomic) BOOL isInEurope;
#end
other using
#interface CountryModel : JSONModel
#property (nonatomic) int id;
#property (nonatomic) NSString* country;
#property (nonatomic) NSString* dialCode;
#property (nonatomic) BOOL isInEurope;
#end
Which is better usage?
Both the methods are correct. Declaration of properties depends on your requirement.Properties are used to declare a class’s accessor methods. How can a class access model's data.While declaring property you can then optionally provide set of property attributes that define the storage semantics and other behaviors of the property.When we are defining an object's property as weak/strong we are defining its accessibility to the class.
It depends on how you need to access the data. If you want you object to thread safe,you can define as nonatomic. By defining it as strong/ assign it defines that you own the object.And by defining it as weak you dont own your object. Check this link for more info.
Hope it helps. Happy Coding!!
I'm using JSONMODEl (https://github.com/icanzilb/JSONModel) to parse a wordpress JSON FEED (with json-api).
Everything go's well except if I want the "comments".
my feed is like that :
comments = (
{
content = "<p>My comment</p>\n";
date = "2014-08-29 20:56:29";
id = 97813;
name = johndoe;
parent = 0;
url = "http://www.google.com";
}
);
so I try to make my "newsmodel" like that :
#import "JSONModel.h"
#import "commentmodel.h"
#protocol NewsModel #end
#interface NewsModel : JSONModel
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) NSString* content;
#property (strong, nonatomic) NSString* thumbnail_images;
#property (strong, nonatomic) NSString* premium;
#property (strong, nonatomic) NSString* id;
#property (strong, nonatomic) CommentModel* comments;
#end
and my commentmodel like that
#import "JSONModel.h"
#interface CommentModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) NSString* content;
#end
But When I try to build my app, my "feed" is empty.
if I comment the "comment" part of the news model, I got the content....
I think I'm stuck somewhere, but where ! If someone ave an idea :)
Many thanks
comments is an array, not a single comment, notice the top level ( and ) which designate an array in a NSDictionary NSLog(). Inside of the is an array element designated by { and }.
But the NewsModel has comments defined as a single comment (CommentModel), not an array. it should probably be declared:
In the docs see Model collections and how products is handled.
You will have to declare a protocol, see the example protocol at the top of the "Model collections" examples.
#protocol CommentModel
#end
Above:
#interface CommentModel : JSONModel
#property (strong, nonatomic) NSArray< CommentModel >* comments;
#protocol CommentModel
#end
#interface CommentModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) NSString* content;
#end
#interface NewsModel : JSONModel
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) NSString* content;
#property (strong, nonatomic) NSString* thumbnail_images;
#property (strong, nonatomic) NSString* premium;
#property (strong, nonatomic) NSString* id; //int?
#property (strong, nonatomic) NSArray<CommentModel>* comments;
#end
Thanks, got it to build, but now If i try to alloc it with
#try {
_feed = [[NewsFeed alloc] initWithDictionary:obj error:nil];
}
#catch (NSException *e) {
NSLog(#"Parse error : %# reason %#", [e name], [e reason]);
}
I got a Bad property protocol declaration reason is not allowed JSONModel property protocol, and not a JSONModel class.
my news feed is like that
#interface NewsFeed : JSONModel
#property (nonatomic, strong) NSArray <NewsModel> *posts;
#end
And work like a charme without the "comment" part...
Thanks
As an addition to the answers above, since I can't add a comment yet, all you have to do, is add an empty protocol with the same name, like this:
#protocol CommentModel
#end
Then, as noted here JsonModel documentation, notation would be different than a notation. The first one is a protocol declaration needed for JsonModel to work, the other one is an objc compiler helper declaration. You can combine them as noted in the same example:
#property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
I have a fairly basic problem with the JSONModel. Let's say I have the following JSON:
{"items": [
{
"id": 1,
"title": "Bla",
"category": 1
}
]}
and this one:
{"categories": [
{
"id": 1,
"name": "Category"
}
]}
Now the easiest thing would be to put the categories inside the items and have JSONModel just use that. But there might be hundreds of items which share just a few categories, and the categories have several attributes like description, URLs and stuff, and that would blow up the items JSON.
How would I combine them in the best way using JSONModel (or might another library be better)?
My models currently look like this:
#interface Item : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) Category* category;
#end
#interface Category : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#end
try this
#protocol Item
#end
#interface Item : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) Category* category;
#end
#interface Items : JSONModel
#property (strong, nonatomic) NSArray<Item> *items;
#end
#protocol Category
#end
#interface Category : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString *name;
#end
#interface Categories : JSONModel
#property (strong, nonatomic) NSArray<Category> *categories;
#end
Your JSON is array of items or categories
I want to add my custom NSObject class to other as property, is that possible?
Class look like this:
#interface PlaceHolder : NSObject
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSString *description;
#property (strong, nonatomic) NSString *webPage;
I want to make it property of my other class to initialize it once, and then work with properties - name, description. webpage and other..
Do i need to create a category? Or there is another way to achieve this?
Any advice would be appreciated, thank you.
Just add a property to the class that needs to use it
#property (strong, nonatomic) PlaceHolder *placeHolder;