Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How to serialize the following class in objective-c so that it can be used with SBJson?

I get "JSON serialisation not supported for Animal" error when I use this code.

Can someone point out where I am going wrong?

The contents of Animal.h file is as below

#import <UIKit/UIKit.h>

@interface Animal : NSObject<NSCoding> {
    NSString *name;
    NSString *description;
    NSString *imageURL;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *imageURL;

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u;

@end

The contents of Animal.m file is as below

#import "Animal.h"

@implementation Animal
@synthesize name, description, imageURL;

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

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

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

@end
like image 727
Billy Samuel Avatar asked Jan 19 '12 05:01

Billy Samuel


1 Answers

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
like image 57
Ilanchezhian Avatar answered Oct 04 '22 14:10

Ilanchezhian