How to serialize a class in IOS sdk (Objective-c)?

安稳与你 提交于 2019-11-30 21:53:18

Make your custom class conform to NSCoding protocol and then serialize it.

For more info, visit the Apple documentation

Also, this link will also help you. As suggested in this link, archive your custom class to NSData and serialize that as provided in the Apple documentation.

Edit Make your Animal.m as follows:

#import "Animal.h"

@implementation Animal
@synthesize name, description, imageURL;

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u {
    self = [super init];
    if( self )
    {
       self.name = n;
       self.description = d;
       self.imageURL = u;
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if( self )
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.description = [aDecoder decodeObjectForKey:@"description"];
        self.imageURL = [aDecoder decodeObjectForKey:@"imageURL"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:name forKey:@"name"];
    [encoder encodeObject:description forKey:@"description"];
    [encoder encodeObject:imageURL forKey:@"imageURL"];
}    

@end

To actually answer your question on how to do it using SBJson: Implement the proxyForJson method. Unless you are serializing an NSArray or NSDictionary you must override this method for serialization to work.

You can see that this is the case by looking at the SBJson source code (in SBJsonWriter.m):

- (NSData*)dataWithObject:(id)object {    
...   
    if ([object isKindOfClass:[NSDictionary class]])
                ok = [streamWriter writeObject:object];

            else if ([object isKindOfClass:[NSArray class]])
                ok = [streamWriter writeArray:object];

            else if ([object respondsToSelector:@selector(proxyForJson)])
                return [self dataWithObject:[object proxyForJson]];
            else {
                self.error = @"Not valid type for JSON";
                return nil;
            }
    ...
    }
}

Implement proxyForJson in Animal.m like this (not tested):

- (NSDictionary*) proxyForJson
{
return [NSDictionary dictionaryWithObjectsAndKeys:self.name, @"name",
                                                  self.description, @"description",
                                                  self.imageURL, @"imageURL", 
                                                  nil];
}

This open source project JSONCoding makes the whole process pretty simple, using the new sdk class in conjunction with the NSCoding protocol.

jcm

Check the newly introduced NSJSONSerialization class:

NSJSONSerialization class

Ballu

I think you can check out this if it helps you: Make a Custom Class Serializable

See also https://github.com/jsonmodel/jsonmodel

Magical Data Modeling Framework for JSON - allows rapid creation of smart data models. You can use it in your iOS, macOS, watchOS and tvOS apps.

This is also the chosen library for the Objective-c Swagger client.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!