在Swift中使用WatchConnectivity / encoding传输自定义对象数组

时间:2015-10-19 15:53:40

标签: swift2 encode nskeyedarchiver nscoder watchconnectivity

我尝试将一组自定义对象从iOS传输到watchkitextension。

理解为了这样做,需要对数据进行编码。虽然在解码时遇到错误。

我们走了:

自定义对象:

private void copyJar(File source, File dest) throws IOException {

        InputStream is = null;
        File dest2 = new File(dest+"/TheJar.jar");
        dest2.createNewFile();
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest2);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer))>0) {
                os.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
        is.close();
        os.close();

    }

包含数组的类:

    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)
    }
}

在这里我得到错误"执行被中断 - 原因信号SIGABRT"

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

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

1 个答案:

答案 0 :(得分:0)

尝试将这些部分更改为:

 private void fetchData() {
        db = helper.getReadableDatabase();
        Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
        adapter = new SimpleCursorAdapter(
                this,
                R.layout.row,
                c,
                new String[]{DBhelper.Name},
                new int[]{R.id.lblreload});
        rldlist.setAdapter(adapter);
    }

这样序列化与反序列化以及Person类所需的类型相匹配。也就是说,WWDC关于WatchConnectivity的讨论特别建议使用NSKeyedArchiver,因为它不是一个非常节省空间的序列化方法。