Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i use NSString Array? #3415

Closed
cdoky opened this issue Apr 7, 2016 · 14 comments
Closed

how can i use NSString Array? #3415

cdoky opened this issue Apr 7, 2016 · 14 comments
Labels

Comments

@cdoky
Copy link

cdoky commented Apr 7, 2016

//=========================
hi, i have a property is a NSString Array, like this
//["tag1", "tag2"]
how can i use RLMObject to write?

thank you

@Kirow
Copy link

Kirow commented Apr 7, 2016

@interface Tag : RLMObject
@property NSString *tagName;
@end

@interface TaggedObject : RLMObject
@propery RLMArray<Tag> *tags;
@end

NSArray *stringArrayOfTags = @[@"tag1", @"tag2"];

RLMRealm *realm = [RLMRealm defaultRealm];

TaggedObject *myNewObject = [TaggedObject new];
for (NSString *name in stringArrayOfTags) {
    Tag *tag = [Tag new];
    tag.tagName = name;

    [myNewObject.tags addObject:tag];
}

[realm beginWriteTransaction];
[realm addObject:myNewObject];
[realm commitWriteTransaction];

@mrackwitz
Copy link
Contributor

@Kirow: Thanks for jumping in here. 👍
@i-phil: Does the sample code help with your question?

@cdoky
Copy link
Author

cdoky commented Apr 7, 2016

@Kirow @mrackwitz thank you,but it can't help me with this question

@interface SHOT : NSObject
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString * rebounds_url;
@property (nonatomic, strong) NSArray<NSString> * tags;
@property (nonatomic, strong) USER * user;
@property (nonatomic, strong) TEAM * team;
@end

this is a json mode for me, i want use realm to cache it, so i changed code to this:
@interface SHOT : RLMObject
and i don't know how to change
@property (nonatomic, strong) NSArray<NSString> * tags;

@beloso
Copy link

beloso commented Apr 7, 2016

You need to transform your JSON object into a realm object.

There is no way around that.

@cdoky
Copy link
Author

cdoky commented Apr 7, 2016

@tiagoveloso you mean is i can't use Realm Object for this JSON Object?

@Kirow
Copy link

Kirow commented Apr 7, 2016

you can use this aproach

@interface SHOT : NSObject <NSCoding> //implement encode & decode methods
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString * rebounds_url;
@property (nonatomic, strong) NSArray * tags;
@property (nonatomic, strong) USER * user;
@property (nonatomic, strong) TEAM * team;
@end

@interface RLMSHOT : RLMObject
@property NSData *shotObject
@end

NSData *shotData = [NSKeyedArchiver archivedDataWithRootObject:yourShotObject];

RLMSHOT *rlmSHOTCache = [RLMSHOT new];
rlmSHOTCache.shotObject = shotData;

SHOT *unarchiveSHOT = [NSKeyedUnarchiver unarchiveObjectWithFile:rlmSHOTCache.shotObject];

if you need additional sort/filter functionality, just extend you realm model

@interface RLMSHOT : RLMObject
@property NSInteger id;
@property NSData *shotObject
@end

NSData *shotData = [NSKeyedArchiver archivedDataWithRootObject:yourShotObject];

RLMSHOT *rlmSHOTCache = [RLMSHOT new];
rlmSHOTCache.shotObject = shotData;
rlmSHOTCache.id = yourShotObject.id;

SHOT *shot = [RLMSHOT objectsWhere:@"id = %@",@21].firstObject;

FastCoding can be useful.

@cdoky
Copy link
Author

cdoky commented Apr 7, 2016

@Kirow thank you, but it's too complicated

@mrackwitz
Copy link
Contributor

@i-phil: It's not exactly clear to me what you want to achieve. If you need further assistance, could you clarify your question so that we can help you better?

@cdoky
Copy link
Author

cdoky commented Apr 7, 2016

@mrackwitz sorrry, my question is: i have a json string, like this:
{"tags":["1", "2"]}
I want to unserialize it as an realm object,

@Kirow
Copy link

Kirow commented Apr 7, 2016

@interface Tag : RLMObject
@property NSString *tagName;
@end

NSData *json = [@"{"tags":["1", "2"]} " dataUsingEncoding:4];
NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:json options:0 error:nil];
NSArray *tags = parsedObject[@"tags"];

RLMRealm *realm = [RLMRealm defaultRealm];

[realm beginWriteTransaction];
for (NSString *name in stringArrayOfTags) {
    Tag *tag = [Tag new];
    tag.tagName = name;
    [realm addObject:tag];
}
[realm commitWriteTransaction];

Not sure that I understood what you want. But if you want to move your SHOT model (deserialized from json) to Realm, see my first answer. It must work.

@cdoky
Copy link
Author

cdoky commented Apr 7, 2016

@Kirow If it is like this? This is a complete json object。
{
"html_url" : "https://dribbble.com/shots/2637319-Hello-Circular",
"projects_url" : "https://api.dribbble.com/v1/shots/2637319/projects",
"team" : {
"html_url" : "https://dribbble.com/Spotify",
"location" : "Stockholm // New York // London",
"pro" : false,
"projects_url" : "https://api.dribbble.com/v1/users/181459/projects",
"following_url" : "https://api.dribbble.com/v1/users/181459/following",
"projects_count" : 0,
"members_count" : 20,
"buckets_url" : "https://api.dribbble.com/v1/users/181459/buckets",
"updated_at" : "2016-04-07T07:23:47Z",
"team_shots_url" : "https://api.dribbble.com/v1/teams/181459/shots",
"shots_count" : 22,
"rebounds_received_count" : 4,
"bio" : "",
"links" : {
"web" : "http://www.spotify.com",
"twitter" : "https://twitter.com/spotifydesign"
},
"name" : "Spotify",
"avatar_url" : "https://d13yacurqjgara.cloudfront.net/users/181459/avatars/normal/8c8e4552d7f149815586e2bf5a533872.jpg?1434458347",
"can_upload_shot" : true,
"type" : "Team",
"shots_url" : "https://api.dribbble.com/v1/users/181459/shots",
"id" : 181459,
"buckets_count" : 0,
"members_url" : "https://api.dribbble.com/v1/teams/181459/members",
"followers_count" : 6526,
"likes_received_count" : 576,
"likes_count" : 8,
"followings_count" : 15,
"created_at" : "2012-07-24T19:08:16Z",
"followers_url" : "https://api.dribbble.com/v1/users/181459/followers",
"comments_received_count" : 65,
"likes_url" : "https://api.dribbble.com/v1/users/181459/likes",
"username" : "Spotify"
},
"title" : "Hello Circular.",
"tags" : [
"brand",
"circular",
"design",
"font",
"spotify",
"typeface",
"ui"
],
"buckets_url" : "https://api.dribbble.com/v1/shots/2637319/buckets",
"updated_at" : "2016-04-07T07:23:47Z",
"animated" : false,
"comments_url" : "https://api.dribbble.com/v1/shots/2637319/comments",
"comments_count" : 17,
"images" : {
"hidpi" : "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still.png",
"teaser" : "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still_teaser.png",
"normal" : "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still_1x.png"
},
"attachments_url" : "https://api.dribbble.com/v1/shots/2637319/attachments",
"rebounds_url" : "https://api.dribbble.com/v1/shots/2637319/rebounds",
"id" : 2637319,
"buckets_count" : 8,
"attachments_count" : 5,
"rebounds_count" : 0,
"user" : {
"html_url" : "https://dribbble.com/hellostanley",
"location" : "London / Stockholm",
"pro" : true,
"projects_url" : "https://api.dribbble.com/v1/users/3883/projects",
"following_url" : "https://api.dribbble.com/v1/users/3883/following",
"projects_count" : 0,
"buckets_url" : "https://api.dribbble.com/v1/users/3883/buckets",
"updated_at" : "2016-04-07T08:16:36Z",
"shots_count" : 2,
"rebounds_received_count" : 0,
"bio" : "British Designer living in Stockholm. Design Director <a href="https://dribbble.com/Spotify">@spotify</a>.",
"links" : {
"web" : "http://www.hellostanley.com",
"twitter" : "https://twitter.com/hellostanley"
},
"name" : "Stanley",
"avatar_url" : "https://d13yacurqjgara.cloudfront.net/users/3883/avatars/normal/8b2a042a54708a8be6036cdb7d4e4de3.jpg?1432991845",
"can_upload_shot" : true,
"type" : "Player",
"shots_url" : "https://api.dribbble.com/v1/users/3883/shots",
"id" : 3883,
"buckets_count" : 0,
"teams_count" : 1,
"teams_url" : "https://api.dribbble.com/v1/users/3883/teams",
"followers_count" : 107,
"likes_received_count" : 591,
"likes_count" : 92,
"followings_count" : 84,
"created_at" : "2010-08-03T10:25:47Z",
"followers_url" : "https://api.dribbble.com/v1/users/3883/followers",
"comments_received_count" : 26,
"likes_url" : "https://api.dribbble.com/v1/users/3883/likes",
"username" : "hellostanley"
},
"height" : 300,
"width" : 400,
"likes_count" : 352,
"created_at" : "2016-04-07T07:16:02Z",
"views_count" : 4362,
"likes_url" : "https://api.dribbble.com/v1/shots/2637319/likes",
"description" : "

Meet Circular, Spotify's new typeface. </p>\n\n

Circular is a friendly sans-serif with unmistakable character and universal appeal. Just the traits we were looking for when choosing a typeface. </p>\n\n

This update is a key part of the brand refresh we introduced last year, and we couldn't be more excited to be rolling it out. The update is now live on iOS and soon to be on Android and Desktop too. We think you’re going to get on really well.</p>\n\n

Looking forward to sharing more with you soon.</p>\n\n

—Spotify Design.</p>"
}

#class
@interface SHOT : RLMObject
@Property (nonatomic, assign) NSInteger id;
@Property (nonatomic, strong) NSString * title;
@Property (nonatomic, strong) NSString * description;
@Property (nonatomic, assign) NSInteger width;
@Property (nonatomic, assign) NSInteger height;
@Property (nonatomic, strong) IMAGE * images;
@Property (nonatomic, assign) NSInteger views_count;
@Property (nonatomic, assign) NSInteger likes_count;
@Property (nonatomic, assign) NSInteger comments_count;
@Property (nonatomic, assign) NSInteger attachments_count;
@Property (nonatomic, assign) NSInteger rebounds_count;
@Property (nonatomic, assign) NSInteger buckets_count;
@Property (nonatomic, strong) NSDate * created_at;
@Property (nonatomic, strong) NSDate * updated_at;
@Property (nonatomic, strong) NSString * html_url;
@Property (nonatomic, strong) NSString * attachments_url;
@Property (nonatomic, strong) NSString * buckets_url;
@Property (nonatomic, strong) NSString * comments_url;
@Property (nonatomic, strong) NSString * likes_url;
@Property (nonatomic, strong) NSString * projects_url;
@Property (nonatomic, strong) NSString * rebounds_url;
@Property (nonatomic, strong) NSArray * tags;
@Property (nonatomic, strong) USER * user;
@Property (nonatomic, strong) TEAM * team;
@EnD

@Kirow
Copy link

Kirow commented Apr 7, 2016

Chalange accepted.

Used this tool and then made minor code fixes

Models

@interface RootClass : RLMObject
@property (nonatomic, assign) BOOL animated;
@property (nonatomic, assign) NSInteger attachmentsCount;
@property (nonatomic, strong) NSString * attachmentsUrl;
@property (nonatomic, assign) NSInteger bucketsCount;
@property (nonatomic, strong) NSString * bucketsUrl;
@property (nonatomic, assign) NSInteger commentsCount;
@property (nonatomic, strong) NSString * commentsUrl;
@property (nonatomic, strong) NSString * createdAt;
@property (nonatomic, strong) NSString * descriptionField;
@property (nonatomic, assign) NSInteger height;
@property (nonatomic, strong) NSString * htmlUrl;
@property (nonatomic, assign) NSInteger idField;
@property (nonatomic, strong) Image * images;
@property (nonatomic, assign) NSInteger likesCount;
@property (nonatomic, strong) NSString * likesUrl;
@property (nonatomic, strong) NSString * projectsUrl;
@property (nonatomic, assign) NSInteger reboundsCount;
@property (nonatomic, strong) NSString * reboundsUrl;
@property (nonatomic, strong) RLMArray<Tag> * tags;
@property (nonatomic, strong) Team * team;
@property (nonatomic, strong) NSString * title;
@property (nonatomic, strong) NSString * updatedAt;
@property (nonatomic, strong) User * user;
@property (nonatomic, assign) NSInteger viewsCount;
@property (nonatomic, assign) NSInteger width;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
RLM_ARRAY_TYPE(RootClass)

@interface Image : RLMObject
@property (nonatomic, strong) NSString * hidpi;
@property (nonatomic, strong) NSString * normal;
@property (nonatomic, strong) NSString * teaser;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
RLM_ARRAY_TYPE(Image)

@interface Tag : RLMObject
@property NSString *name;
@end
RLM_ARRAY_TYPE(Tag)

@interface Team : RLMObject
@property (nonatomic, strong) NSString * avatarUrl;
@property (nonatomic, strong) NSString * bio;
@property (nonatomic, assign) NSInteger bucketsCount;
@property (nonatomic, strong) NSString * bucketsUrl;
@property (nonatomic, assign) BOOL canUploadShot;
@property (nonatomic, assign) NSInteger commentsReceivedCount;
@property (nonatomic, strong) NSString * createdAt;
@property (nonatomic, assign) NSInteger followersCount;
@property (nonatomic, strong) NSString * followersUrl;
@property (nonatomic, strong) NSString * followingUrl;
@property (nonatomic, assign) NSInteger followingsCount;
@property (nonatomic, strong) NSString * htmlUrl;
@property (nonatomic, assign) NSInteger idField;
@property (nonatomic, assign) NSInteger likesCount;
@property (nonatomic, assign) NSInteger likesReceivedCount;
@property (nonatomic, strong) NSString * likesUrl;
@property (nonatomic, strong) Link * links;
@property (nonatomic, strong) NSString * location;
@property (nonatomic, assign) NSInteger membersCount;
@property (nonatomic, strong) NSString * membersUrl;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, assign) BOOL pro;
@property (nonatomic, assign) NSInteger projectsCount;
@property (nonatomic, strong) NSString * projectsUrl;
@property (nonatomic, assign) NSInteger reboundsReceivedCount;
@property (nonatomic, assign) NSInteger shotsCount;
@property (nonatomic, strong) NSString * shotsUrl;
@property (nonatomic, strong) NSString * teamShotsUrl;
@property (nonatomic, strong) NSString * type;
@property (nonatomic, strong) NSString * updatedAt;
@property (nonatomic, strong) NSString * username;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
RLM_ARRAY_TYPE(Team)

@interface Link : RLMObject
@property (nonatomic, strong) NSString * twitter;
@property (nonatomic, strong) NSString * web;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
RLM_ARRAY_TYPE(Link)

@interface User : RLMObject
@property (nonatomic, strong) NSString * avatarUrl;
@property (nonatomic, strong) NSString * bio;
@property (nonatomic, assign) NSInteger bucketsCount;
@property (nonatomic, strong) NSString * bucketsUrl;
@property (nonatomic, assign) BOOL canUploadShot;
@property (nonatomic, assign) NSInteger commentsReceivedCount;
@property (nonatomic, strong) NSString * createdAt;
@property (nonatomic, assign) NSInteger followersCount;
@property (nonatomic, strong) NSString * followersUrl;
@property (nonatomic, strong) NSString * followingUrl;
@property (nonatomic, assign) NSInteger followingsCount;
@property (nonatomic, strong) NSString * htmlUrl;
@property (nonatomic, assign) NSInteger idField;
@property (nonatomic, assign) NSInteger likesCount;
@property (nonatomic, assign) NSInteger likesReceivedCount;
@property (nonatomic, strong) NSString * likesUrl;
@property (nonatomic, strong) Link * links;
@property (nonatomic, strong) NSString * location;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, assign) BOOL pro;
@property (nonatomic, assign) NSInteger projectsCount;
@property (nonatomic, strong) NSString * projectsUrl;
@property (nonatomic, assign) NSInteger reboundsReceivedCount;
@property (nonatomic, assign) NSInteger shotsCount;
@property (nonatomic, strong) NSString * shotsUrl;
@property (nonatomic, assign) NSInteger teamsCount;
@property (nonatomic, strong) NSString * teamsUrl;
@property (nonatomic, strong) NSString * type;
@property (nonatomic, strong) NSString * updatedAt;
@property (nonatomic, strong) NSString * username;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
@implementation RootClass

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"animated"] isKindOfClass:[NSNull class]]){
        self.animated = [dictionary[@"animated"] boolValue];
    }

    if(![dictionary[@"attachments_count"] isKindOfClass:[NSNull class]]){
        self.attachmentsCount = [dictionary[@"attachments_count"] integerValue];
    }

    if(![dictionary[@"attachments_url"] isKindOfClass:[NSNull class]]){
        self.attachmentsUrl = dictionary[@"attachments_url"];
    }

    if(![dictionary[@"buckets_count"] isKindOfClass:[NSNull class]]){
        self.bucketsCount = [dictionary[@"buckets_count"] integerValue];
    }

    if(![dictionary[@"buckets_url"] isKindOfClass:[NSNull class]]){
        self.bucketsUrl = dictionary[@"buckets_url"];
    }

    if(![dictionary[@"comments_count"] isKindOfClass:[NSNull class]]){
        self.commentsCount = [dictionary[@"comments_count"] integerValue];
    }

    if(![dictionary[@"comments_url"] isKindOfClass:[NSNull class]]){
        self.commentsUrl = dictionary[@"comments_url"];
    }

    if(![dictionary[@"created_at"] isKindOfClass:[NSNull class]]){
        self.createdAt = dictionary[@"created_at"];
    }

    if(![dictionary[@"description"] isKindOfClass:[NSNull class]]){
        self.descriptionField = dictionary[@"description"];
    }

    if(![dictionary[@"height"] isKindOfClass:[NSNull class]]){
        self.height = [dictionary[@"height"] integerValue];
    }

    if(![dictionary[@"html_url"] isKindOfClass:[NSNull class]]){
        self.htmlUrl = dictionary[@"html_url"];
    }

    if(![dictionary[@"id"] isKindOfClass:[NSNull class]]){
        self.idField = [dictionary[@"id"] integerValue];
    }

    if(![dictionary[@"images"] isKindOfClass:[NSNull class]]){
        self.images = [[Image alloc] initWithDictionary:dictionary[@"images"]];
    }

    if(![dictionary[@"likes_count"] isKindOfClass:[NSNull class]]){
        self.likesCount = [dictionary[@"likes_count"] integerValue];
    }

    if(![dictionary[@"likes_url"] isKindOfClass:[NSNull class]]){
        self.likesUrl = dictionary[@"likes_url"];
    }

    if(![dictionary[@"projects_url"] isKindOfClass:[NSNull class]]){
        self.projectsUrl = dictionary[@"projects_url"];
    }

    if(![dictionary[@"rebounds_count"] isKindOfClass:[NSNull class]]){
        self.reboundsCount = [dictionary[@"rebounds_count"] integerValue];
    }

    if(![dictionary[@"rebounds_url"] isKindOfClass:[NSNull class]]){
        self.reboundsUrl = dictionary[@"rebounds_url"];
    }

    if(![dictionary[@"tags"] isKindOfClass:[NSNull class]]){
        NSArray *tags = dictionary[@"tags"];
        for (NSString *tag in tags) {
            Tag *rlmTag = [Tag new];
            rlmTag.name = tag;
            [self.tags addObject:rlmTag];
        }
    }

    if(![dictionary[@"team"] isKindOfClass:[NSNull class]]){
        self.team = [[Team alloc] initWithDictionary:dictionary[@"team"]];
    }

    if(![dictionary[@"title"] isKindOfClass:[NSNull class]]){
        self.title = dictionary[@"title"];
    }

    if(![dictionary[@"updated_at"] isKindOfClass:[NSNull class]]){
        self.updatedAt = dictionary[@"updated_at"];
    }

    if(![dictionary[@"user"] isKindOfClass:[NSNull class]]){
        self.user = [[User alloc] initWithDictionary:dictionary[@"user"]];
    }

    if(![dictionary[@"views_count"] isKindOfClass:[NSNull class]]){
        self.viewsCount = [dictionary[@"views_count"] integerValue];
    }

    if(![dictionary[@"width"] isKindOfClass:[NSNull class]]){
        self.width = [dictionary[@"width"] integerValue];
    }

    return self;
}
@end

@implementation Image

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"hidpi"] isKindOfClass:[NSNull class]]){
        self.hidpi = dictionary[@"hidpi"];
    }

    if(![dictionary[@"normal"] isKindOfClass:[NSNull class]]){
        self.normal = dictionary[@"normal"];
    }

    if(![dictionary[@"teaser"] isKindOfClass:[NSNull class]]){
        self.teaser = dictionary[@"teaser"];
    }

    return self;
}
@end

@implementation Tag
@end

@implementation Team

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"avatar_url"] isKindOfClass:[NSNull class]]){
        self.avatarUrl = dictionary[@"avatar_url"];
    }

    if(![dictionary[@"bio"] isKindOfClass:[NSNull class]]){
        self.bio = dictionary[@"bio"];
    }

    if(![dictionary[@"buckets_count"] isKindOfClass:[NSNull class]]){
        self.bucketsCount = [dictionary[@"buckets_count"] integerValue];
    }

    if(![dictionary[@"buckets_url"] isKindOfClass:[NSNull class]]){
        self.bucketsUrl = dictionary[@"buckets_url"];
    }

    if(![dictionary[@"can_upload_shot"] isKindOfClass:[NSNull class]]){
        self.canUploadShot = [dictionary[@"can_upload_shot"] boolValue];
    }

    if(![dictionary[@"comments_received_count"] isKindOfClass:[NSNull class]]){
        self.commentsReceivedCount = [dictionary[@"comments_received_count"] integerValue];
    }

    if(![dictionary[@"created_at"] isKindOfClass:[NSNull class]]){
        self.createdAt = dictionary[@"created_at"];
    }

    if(![dictionary[@"followers_count"] isKindOfClass:[NSNull class]]){
        self.followersCount = [dictionary[@"followers_count"] integerValue];
    }

    if(![dictionary[@"followers_url"] isKindOfClass:[NSNull class]]){
        self.followersUrl = dictionary[@"followers_url"];
    }

    if(![dictionary[@"following_url"] isKindOfClass:[NSNull class]]){
        self.followingUrl = dictionary[@"following_url"];
    }

    if(![dictionary[@"followings_count"] isKindOfClass:[NSNull class]]){
        self.followingsCount = [dictionary[@"followings_count"] integerValue];
    }

    if(![dictionary[@"html_url"] isKindOfClass:[NSNull class]]){
        self.htmlUrl = dictionary[@"html_url"];
    }

    if(![dictionary[@"id"] isKindOfClass:[NSNull class]]){
        self.idField = [dictionary[@"id"] integerValue];
    }

    if(![dictionary[@"likes_count"] isKindOfClass:[NSNull class]]){
        self.likesCount = [dictionary[@"likes_count"] integerValue];
    }

    if(![dictionary[@"likes_received_count"] isKindOfClass:[NSNull class]]){
        self.likesReceivedCount = [dictionary[@"likes_received_count"] integerValue];
    }

    if(![dictionary[@"likes_url"] isKindOfClass:[NSNull class]]){
        self.likesUrl = dictionary[@"likes_url"];
    }

    if(![dictionary[@"links"] isKindOfClass:[NSNull class]]){
        self.links = [[Link alloc] initWithDictionary:dictionary[@"links"]];
    }

    if(![dictionary[@"location"] isKindOfClass:[NSNull class]]){
        self.location = dictionary[@"location"];
    }

    if(![dictionary[@"members_count"] isKindOfClass:[NSNull class]]){
        self.membersCount = [dictionary[@"members_count"] integerValue];
    }

    if(![dictionary[@"members_url"] isKindOfClass:[NSNull class]]){
        self.membersUrl = dictionary[@"members_url"];
    }

    if(![dictionary[@"name"] isKindOfClass:[NSNull class]]){
        self.name = dictionary[@"name"];
    }

    if(![dictionary[@"pro"] isKindOfClass:[NSNull class]]){
        self.pro = [dictionary[@"pro"] boolValue];
    }

    if(![dictionary[@"projects_count"] isKindOfClass:[NSNull class]]){
        self.projectsCount = [dictionary[@"projects_count"] integerValue];
    }

    if(![dictionary[@"projects_url"] isKindOfClass:[NSNull class]]){
        self.projectsUrl = dictionary[@"projects_url"];
    }

    if(![dictionary[@"rebounds_received_count"] isKindOfClass:[NSNull class]]){
        self.reboundsReceivedCount = [dictionary[@"rebounds_received_count"] integerValue];
    }

    if(![dictionary[@"shots_count"] isKindOfClass:[NSNull class]]){
        self.shotsCount = [dictionary[@"shots_count"] integerValue];
    }

    if(![dictionary[@"shots_url"] isKindOfClass:[NSNull class]]){
        self.shotsUrl = dictionary[@"shots_url"];
    }

    if(![dictionary[@"team_shots_url"] isKindOfClass:[NSNull class]]){
        self.teamShotsUrl = dictionary[@"team_shots_url"];
    }

    if(![dictionary[@"type"] isKindOfClass:[NSNull class]]){
        self.type = dictionary[@"type"];
    }

    if(![dictionary[@"updated_at"] isKindOfClass:[NSNull class]]){
        self.updatedAt = dictionary[@"updated_at"];
    }

    if(![dictionary[@"username"] isKindOfClass:[NSNull class]]){
        self.username = dictionary[@"username"];
    }

    return self;
}
@end

@implementation Link

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"twitter"] isKindOfClass:[NSNull class]]){
        self.twitter = dictionary[@"twitter"];
    }

    if(![dictionary[@"web"] isKindOfClass:[NSNull class]]){
        self.web = dictionary[@"web"];
    }

    return self;
}
@end

@implementation User

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if(dictionary == nil || [dictionary isKindOfClass:[NSNull class]]){
        return nil;
    }
    self = [super init];

    if(![dictionary[@"avatar_url"] isKindOfClass:[NSNull class]]){
        self.avatarUrl = dictionary[@"avatar_url"];
    }

    if(![dictionary[@"bio"] isKindOfClass:[NSNull class]]){
        self.bio = dictionary[@"bio"];
    }

    if(![dictionary[@"buckets_count"] isKindOfClass:[NSNull class]]){
        self.bucketsCount = [dictionary[@"buckets_count"] integerValue];
    }

    if(![dictionary[@"buckets_url"] isKindOfClass:[NSNull class]]){
        self.bucketsUrl = dictionary[@"buckets_url"];
    }

    if(![dictionary[@"can_upload_shot"] isKindOfClass:[NSNull class]]){
        self.canUploadShot = [dictionary[@"can_upload_shot"] boolValue];
    }

    if(![dictionary[@"comments_received_count"] isKindOfClass:[NSNull class]]){
        self.commentsReceivedCount = [dictionary[@"comments_received_count"] integerValue];
    }

    if(![dictionary[@"created_at"] isKindOfClass:[NSNull class]]){
        self.createdAt = dictionary[@"created_at"];
    }

    if(![dictionary[@"followers_count"] isKindOfClass:[NSNull class]]){
        self.followersCount = [dictionary[@"followers_count"] integerValue];
    }

    if(![dictionary[@"followers_url"] isKindOfClass:[NSNull class]]){
        self.followersUrl = dictionary[@"followers_url"];
    }

    if(![dictionary[@"following_url"] isKindOfClass:[NSNull class]]){
        self.followingUrl = dictionary[@"following_url"];
    }

    if(![dictionary[@"followings_count"] isKindOfClass:[NSNull class]]){
        self.followingsCount = [dictionary[@"followings_count"] integerValue];
    }

    if(![dictionary[@"html_url"] isKindOfClass:[NSNull class]]){
        self.htmlUrl = dictionary[@"html_url"];
    }

    if(![dictionary[@"id"] isKindOfClass:[NSNull class]]){
        self.idField = [dictionary[@"id"] integerValue];
    }

    if(![dictionary[@"likes_count"] isKindOfClass:[NSNull class]]){
        self.likesCount = [dictionary[@"likes_count"] integerValue];
    }

    if(![dictionary[@"likes_received_count"] isKindOfClass:[NSNull class]]){
        self.likesReceivedCount = [dictionary[@"likes_received_count"] integerValue];
    }

    if(![dictionary[@"likes_url"] isKindOfClass:[NSNull class]]){
        self.likesUrl = dictionary[@"likes_url"];
    }

    if(![dictionary[@"links"] isKindOfClass:[NSNull class]]){
        self.links = [[Link alloc] initWithDictionary:dictionary[@"links"]];
    }

    if(![dictionary[@"location"] isKindOfClass:[NSNull class]]){
        self.location = dictionary[@"location"];
    }

    if(![dictionary[@"name"] isKindOfClass:[NSNull class]]){
        self.name = dictionary[@"name"];
    }

    if(![dictionary[@"pro"] isKindOfClass:[NSNull class]]){
        self.pro = [dictionary[@"pro"] boolValue];
    }

    if(![dictionary[@"projects_count"] isKindOfClass:[NSNull class]]){
        self.projectsCount = [dictionary[@"projects_count"] integerValue];
    }

    if(![dictionary[@"projects_url"] isKindOfClass:[NSNull class]]){
        self.projectsUrl = dictionary[@"projects_url"];
    }

    if(![dictionary[@"rebounds_received_count"] isKindOfClass:[NSNull class]]){
        self.reboundsReceivedCount = [dictionary[@"rebounds_received_count"] integerValue];
    }

    if(![dictionary[@"shots_count"] isKindOfClass:[NSNull class]]){
        self.shotsCount = [dictionary[@"shots_count"] integerValue];
    }

    if(![dictionary[@"shots_url"] isKindOfClass:[NSNull class]]){
        self.shotsUrl = dictionary[@"shots_url"];
    }

    if(![dictionary[@"teams_count"] isKindOfClass:[NSNull class]]){
        self.teamsCount = [dictionary[@"teams_count"] integerValue];
    }

    if(![dictionary[@"teams_url"] isKindOfClass:[NSNull class]]){
        self.teamsUrl = dictionary[@"teams_url"];
    }

    if(![dictionary[@"type"] isKindOfClass:[NSNull class]]){
        self.type = dictionary[@"type"];
    }

    if(![dictionary[@"updated_at"] isKindOfClass:[NSNull class]]){
        self.updatedAt = dictionary[@"updated_at"];
    }

    if(![dictionary[@"username"] isKindOfClass:[NSNull class]]){
        self.username = dictionary[@"username"];
    }

    return self;
}
@end

JSON

{
  "html_url": "https://dribbble.com/shots/2637319-Hello-Circular",
  "projects_url": "https://api.dribbble.com/v1/shots/2637319/projects",
  "team": {
    "html_url": "https://dribbble.com/Spotify",
    "location": "Stockholm // New York // London",
    "pro": false,
    "projects_url": "https://api.dribbble.com/v1/users/181459/projects",
    "following_url": "https://api.dribbble.com/v1/users/181459/following",
    "projects_count": 0,
    "members_count": 20,
    "buckets_url": "https://api.dribbble.com/v1/users/181459/buckets",
    "updated_at": "2016-04-07T07:23:47Z",
    "team_shots_url": "https://api.dribbble.com/v1/teams/181459/shots",
    "shots_count": 22,
    "rebounds_received_count": 4,
    "bio": "",
    "links": {
      "web": "http://www.spotify.com",
      "twitter": "https://twitter.com/spotifydesign"
    },
    "name": "Spotify",
    "avatar_url": "https://d13yacurqjgara.cloudfront.net/users/181459/avatars/normal/8c8e4552d7f149815586e2bf5a533872.jpg?1434458347",
    "can_upload_shot": true,
    "type": "Team",
    "shots_url": "https://api.dribbble.com/v1/users/181459/shots",
    "id": 181459,
    "buckets_count": 0,
    "members_url": "https://api.dribbble.com/v1/teams/181459/members",
    "followers_count": 6526,
    "likes_received_count": 576,
    "likes_count": 8,
    "followings_count": 15,
    "created_at": "2012-07-24T19:08:16Z",
    "followers_url": "https://api.dribbble.com/v1/users/181459/followers",
    "comments_received_count": 65,
    "likes_url": "https://api.dribbble.com/v1/users/181459/likes",
    "username": "Spotify"
  },
  "title": "Hello Circular.",
  "tags": [
    "brand",
    "circular",
    "design",
    "font",
    "spotify",
    "typeface",
    "ui"
  ],
  "buckets_url": "https://api.dribbble.com/v1/shots/2637319/buckets",
  "updated_at": "2016-04-07T07:23:47Z",
  "animated": false,
  "comments_url": "https://api.dribbble.com/v1/shots/2637319/comments",
  "comments_count": 17,
  "images": {
    "hidpi": "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still.png",
    "teaser": "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still_teaser.png",
    "normal": "https://d13yacurqjgara.cloudfront.net/users/3883/screenshots/2637319/still_1x.png"
  },
  "attachments_url": "https://api.dribbble.com/v1/shots/2637319/attachments",
  "rebounds_url": "https://api.dribbble.com/v1/shots/2637319/rebounds",
  "id": 2637319,
  "buckets_count": 8,
  "attachments_count": 5,
  "rebounds_count": 0,
  "user": {
    "html_url": "https://dribbble.com/hellostanley",
    "location": "London / Stockholm",
    "pro": true,
    "projects_url": "https://api.dribbble.com/v1/users/3883/projects",
    "following_url": "https://api.dribbble.com/v1/users/3883/following",
    "projects_count": 0,
    "buckets_url": "https://api.dribbble.com/v1/users/3883/buckets",
    "updated_at": "2016-04-07T08:16:36Z",
    "shots_count": 2,
    "rebounds_received_count": 0,
    "bio": "British Designer living in Stockholm. Design Director @Spotify</a>.",
    "links": {
      "web": "http://www.hellostanley.com",
      "twitter": "https://twitter.com/hellostanley"
    },
    "name": "Stanley",
    "avatar_url": "https://d13yacurqjgara.cloudfront.net/users/3883/avatars/normal/8b2a042a54708a8be6036cdb7d4e4de3.jpg?1432991845",
    "can_upload_shot": true,
    "type": "Player",
    "shots_url": "https://api.dribbble.com/v1/users/3883/shots",
    "id": 3883,
    "buckets_count": 0,
    "teams_count": 1,
    "teams_url": "https://api.dribbble.com/v1/users/3883/teams",
    "followers_count": 107,
    "likes_received_count": 591,
    "likes_count": 92,
    "followings_count": 84,
    "created_at": "2010-08-03T10:25:47Z",
    "followers_url": "https://api.dribbble.com/v1/users/3883/followers",
    "comments_received_count": 26,
    "likes_url": "https://api.dribbble.com/v1/users/3883/likes",
    "username": "hellostanley"
  },
  "height": 300,
  "width": 400,
  "likes_count": 352,
  "created_at": "2016-04-07T07:16:02Z",
  "views_count": 4362,
  "likes_url": "https://api.dribbble.com/v1/shots/2637319/likes",
  "description": "ANY DESCRIPTION HERE"
}

Usage

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:myData options:0 error:nil];   
RootClass* spot = [[RootClass alloc] initWithDictionary:parsedObject];

RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
    [realm addObject:spot];
}];

@cdoky
Copy link
Author

cdoky commented Apr 8, 2016

@Kirow yes,i know, but my json to Model is auto complete

STINetwork

I just want to combine Realm and AutoJson2model tool

if,no way to do this. i'll tell server, please don't send StringArray, just send like this {"tag":"tag1,tag2,tag3"}, thank you

@mrackwitz
Copy link
Contributor

@i-phil: I'm going to close this issue now. From your last message it looks like you found a way to resolve your question?
Thanks again @Kirow for your overwhelming assistance here.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants