如何在IOS sdk(Objective-c)中序列化一个类? [英] How to serialize a class in IOS sdk (Objective-c)?

查看:99
本文介绍了如何在IOS sdk(Objective-c)中序列化一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在objective-c中序列化以下类,以便它可以与SBJson一起使用?

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

我得到动物不支持JSON序列化错误使用此代码。

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

有人可以指出我出错的地方吗?

Can someone point out where I am going wrong?

Animal.h的内容文件如下

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

Animal.m文件的内容如下所示

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


推荐答案

使您的自定义类符合 NSCoding 协议,然后将其序列化。

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

有关详细信息,请访问 Apple文档

For more info, visit the Apple documentation

此外,链接也会对您有所帮助。
根据此链接的建议,将自定义类存档到NSData并按照Apple文档中的说明对其进行序列化。

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.

编辑
按如下方式设置 Animal.m

#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

这篇关于如何在IOS sdk(Objective-c)中序列化一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆