2013-01-31 31 views
9

我有這個枚舉:如何在枚舉中使用NSCoding協議?

typedef types { 
    HBIntineraryTypeVisited = 0, 
    HBIntineraryTypeUnvisited, 
    HBIntineraryTypeUnknown, 
    HBIntineraryTypeDeleted, 
} HBIntineraryType; 

,並希望將其存儲使用NSCoding協議

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super init]; 
    if (self) { 
     _name = [aDecoder decodeObjectForKey:@"name"]; 
     // todo decode enum object 
    } 
} 

- (void)encodeWithCoder:(NSCoder *)aCoder 
{ 
    [aCoder encodeObject:_name forKey:@"name"]; 
    // todo encode enum object 
} 

什麼打碼機方法其它一些變量一起做我使用解碼和編碼這種枚舉的?

+1

'decodeInt' /'encodeInt'(與某些投射)。 – trojanfoe

回答

4

一般而言,enum的表示可以變化。在使用Objective-C時,應該使用the NS_ENUM macro來確定哪種類型用於表示枚舉。有更多的背景this article

+2

這是將鏈接作爲答案的缺點。你永遠無法知道他們什麼時候開始顯示404 :( – Kaunteya

+0

)大多數來自Big Nerd Ranch的博客文章都被移動了。以下是新的網址:https://www.bignerdranch.com/blog/enum-num-num/ – Dustt