2012-01-19 16 views
4

如何序列化Objective-C中的以下類,以便它可以與SBJson一起使用?如何在IOS sdk(Objective-c)中序列化一個類?

當我使用此代碼時,出現「Animal不支持JSON序列化」錯誤。

有人可以指出我要去哪裏嗎?

Animal.h文件的內容是如下

#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文件的內容是如下

#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 

回答

7

讓您的自定義類符合NSCoding協議,然後序列化。

欲瞭解更多信息,請訪問Apple documentation

此外,這也link會幫助你。 正如此鏈接中所建議的,將您的自定義類歸檔到NSData並按照Apple文檔中的說明序列化。

編輯 讓你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 
+0

我已經更新了我的答案。檢查.. – Ilanchezhian

+0

第二個環節是關鍵。你使用'[NSKeyedArchiver archivedDataWithRootObject:(id)]'和'[NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)]'來序列化和反序列化對象。 – respectTheCode

1
+0

我添加了以下方法,在「在Objective-c/iPhone中創建自定義類可序列化?」的帖子中說過,但仍然收到錯誤消息,因爲「JSON序列化不支持動物」 - (id) initWithCoder:(NSCoder *)aDecoder; - (void)encodeWithCoder:(NSCoder *)encoder; –

+0

你是否讓這個類符合'NSCoding'協議? – Ilanchezhian

+0

我是新手,爲iPhone應用程序編寫代碼。你可以在我的原始問題中查看源代碼並指出我的錯在哪裏? –

2

我想你可以看看這個,如果它可以幫助你:Make a Custom Class Serializable

+0

我已經在這個鏈接中添加了以下方法,但是仍然收到錯誤消息,因爲「動物不支持JSON序列化」。(ID)initWithCoder:(NSCoder *)aDecoder; - (void)encodeWithCoder:(NSCoder *)encoder; –

+0

我認爲nscoding是一個協議,我們應該在這種方式實施,但如果你沒有得到我的觀點,然後chekc出這一個http://www.raywenderlich.com/1914/how-to-save-your-app -data-with-nscoding-and-nsfilemanager這裏的目的是不同的教程,但使用nscoding你可以從這裏學習我認爲... – Ballu

+0

我已經更新了源代碼的原始問題以及建議的更改您分享的鏈接。 –

4

要真正回答你關於如何使用SBJson做問題:實施方法proxyForJson。除非要序列化NSArrayNSDictionary,否則必須重寫此方法以使序列化正常工作。

你可以看到,這樣的話通過查看SBJson源代碼(在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; 
      } 
    ... 
    } 
} 

Animal.m這樣的(未測試)實施proxyForJson

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

這個開源項目JSONCoding使整個過程非常簡單,使用新的sdk類和NSCoding協議。