2015-10-19 50 views
0

正在嘗試將自定義對象數組從iOS傳輸到watchkitextension。在Swift中使用WatchConnectivity /編碼傳輸自定義對象數組

瞭解到爲了這樣做,數據需要進行編碼。雖然解碼時會出錯。

這裏,我們去:

自定義對象:

final class Person: NSObject { 
     var PersonName:String = "" 
     var PersonAge:Int = 0 
     var joined:NSDate = NSDate() 

     init(PersonName: String, PersonAge:Int, joined:NSDate){ 
      self.PersonName = PersonName 
      self.PersonAge = PersonAge 
      self.joined = joined 
      super.init() 
     } 
    } 

    extension Person: NSCoding { 
     private struct CodingKeys { 
      static let PersonName = "PersonName" 
      static let PersonAge = "PersonAge" 
      static let joined = "joined" 
     } 

     convenience init(coder aDecoder: NSCoder) { 
      let PersonName = aDecoder.decodeObjectForKey(CodingKeys.PersonName) as! String 
      let PersonAge = aDecoder.decodeIntForKey(CodingKeys.PersonAge) as! Int 
      let joined = aDecoder.decodeDoubleForKey(CodingKeys.joined) as! NSDate 

      self.init(PersonName: PersonName, PersonAge: PersonAge, joined: joined) 
     } 

     func encodeWithCoder(encoder: NSCoder) { 
      encoder.encodeObject(PersonName, forKey: CodingKeys.PersonName) 
      encoder.encodeObject(PersonAge, forKey: CodingKeys.PersonAge) 
      encoder.encodeObject(joined, forKey: CodingKeys.joined) 
     } 
    } 

與數組類:

@objc(Group) 
final class Group: NSObject { 

    static let sharedInstance = Group() 

    var Persons:[Person] = [] 

    required override init() { 
     super.init() 
    } 

    init (Persons:[Person]){ 
     self.Persons = Persons 
     super.init() 
    } 

} 

extension Group: NSCoding { 
    private struct CodingKeys { 
     static let Persons = "Persons" 
    } 

    convenience init(coder aDecoder: NSCoder) { 

     let Persons = aDecoder.decodeObjectForKey(CodingKeys.Persons) as! [Person] 
     self.init(Persons: Persons) 

     self.Persons = aDecoder.decodeObjectForKey(CodingKeys.Persons) as! [Person] 
    } 

    func encodeWithCoder(encoder: NSCoder) { 
     encoder.encodeObject(Persons, forKey: CodingKeys.Persons) 
    } 
} 

創建示例對象,追加到數組,然後對其進行編碼:

let aPerson:Person? = Person(PersonName: "Martin", PersonAge: 50, joined: NSDate()) 

Group.sharedInstance.Persons.append(aPerson!) 
let encodedData = NSKeyedArchiver.archivedDataWithRootObject(Group.sharedInstance) 

在這裏,我得到「的執行被中斷 - 的原因信號SIGABRT」錯誤

let decodedData = NSKeyedUnarchiver.unarchiveObjectWithData(encodedData) as? Group 

回答

0

嘗試改變這些部分:

convenience init(coder aDecoder: NSCoder) { 
    let PersonName = aDecoder.decodeObjectForKey(CodingKeys.PersonName) as! String 
    let PersonAge = aDecoder.decodeIntForKey(CodingKeys.PersonAge) as! Int 
    let joined = aDecoder.decodeObjectForKey(CodingKeys.joined) as! NSDate 

    self.init(PersonName: PersonName, PersonAge: PersonAge, joined: joined) 
} 

func encodeWithCoder(encoder: NSCoder) { 
    encoder.encodeObject(PersonName, forKey: CodingKeys.PersonName) 
    encoder.encodeInt(PersonAge, forKey: CodingKeys.PersonAge) 
    encoder.encodeObject(joined, forKey: CodingKeys.joined) 
} 

這樣的序列化反序列化,並通過所需的類型相匹配人員類。也就是說,WWDC在WatchConnectivity上的談話特別推薦使用而不是來使用NSKeyedArchiver,因爲它不是一個非常節省空間的序列化方法。

+0

謝謝你突出顯示這種不匹配。不幸的是仍然是同樣的問如果不是NSKeyedArchiver,你使用什麼? (我試過JSON解析直接從watchkitextension,效果很好的模擬器,但在物理設備超時,所以現在我恢復到執行從手機解析,然後傳遞數據) – TPeter

+0

NSPropertyListSerialization是由WatchConnectivity工程師推薦者之一。只要確保只使用屬性列表類型(它看起來像你)。基本上你想補充一個' - (NSDictionary的*)dictRepresentation'和' - (ID)initWithDictionaryRepresentation:(NSDictionary的*)dictionaryRepresentation'到您的Person類。 – ccjensen

+0

如果您仍然遇到問題,您必須在代碼中的其他位置有bug。由於這個過程正在崩潰,你應該得到生成的崩潰報告,它會告訴你到底發生了什麼問題。 – ccjensen

相關問題