2016-02-11 25 views
0

進口的UIKitNSCoding encodeObject錯誤

類的事件:NSObject的,NSCoding {

// MARK: Properties 
var name: String 
var created_at: String 
var stands:[Stand?] 

// MARK: Archiving Paths 

static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! 
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("events") 


// MARK: Types 

struct PropertyKey { 
    static let nameKey = "name" 
    static let createdAtKey = "created_at" 
    static let standsAtKey = "stands" 

} 

// MARK: Initialization 
init?(name: String, created_at: String, stands:[Stand?]) { 
    // Initialize stored properties. 
    self.name = name 
    self.created_at = created_at 
    self.stands = stands 

    super.init() 

    // Initialization should fail if there is no name or if no created_at. 
    if name.isEmpty || created_at.isEmpty { 
     return nil 
    } 
} 

// MARK: NSCoding 
func encodeWithCoder(aCoder: NSCoder) { 
    aCoder.encodeObject(name, forKey: PropertyKey.nameKey) 
    aCoder.encodeObject(created_at, forKey: PropertyKey.createdAtKey) 
    aCoder.encodeObject(stands, forKey: PropertyKey.standsAtKey) 

} 

required convenience init?(coder aDecoder: NSCoder) { 
    let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String 

    let created_at = aDecoder.decodeObjectForKey(PropertyKey.createdAtKey) as! String 

    let stands = aDecoder.decodeObjectForKey(PropertyKey.standsAtKey) as! [Stand?] 

    // Must call designated initializer. 
    self.init(name: name, created_at: created_at, stands: stands) 
} 

}

我有錯誤從 「aCoder.encodeObject(站,forKey:PropertyKey.standsAtKey)」說:「不能將[Stand?]類型的值轉換爲期望的參數類型'AnyObject?'」

我正在使用NSCoding來保存對象與數組空的立場,然後檢索它,更新此類的事件屬性(事件)

+0

你可以添加'Stand'的代碼? – tktsubota

回答

0

除非您的類「Stand」也符合NSCoding,否則不能對自定義類「Event」進行編碼。