Encoding an array of tuples with a NSCoder


(Saagar Jha) #1

Hello,

I’ve been working on migrating some old code over to Swift 3, and I’m having some trouble archiving an array of tuples:

class Foo: NSObject, NSCoding {
  var bar: [(string1: String, string2: String)]

  required init?(coder aDecoder: NSCoder) {
    bar = aDecoder.decodeObject(forKey: “bar”) as? [(string1: String, string2: String)] ?? []
  }

  func encode(with aCoder: NSCoder) {
    aCoder.encode(bar, forKey: “bar”) // crash
  }
}

Unfortunately, this code doesn’t seem to work anymore. Is there any way to get a array of tuple encoded without resorting to creating a struct or class in its place?

Thanks,
Saagar Jha


(Philippe Hausler) #2

NSCoding has never worked with either tuples or classes correctly (primarily because it is not really designed to do so). I would suggest to encode and decode either as an array of array of strings and convert or perhaps encode/decode as an array of classes representing the meaning of the tuple.

···

On Sep 22, 2016, at 2:07 PM, Saagar Jha via swift-users <swift-users@swift.org> wrote:

Hello,

I’ve been working on migrating some old code over to Swift 3, and I’m having some trouble archiving an array of tuples:

class Foo: NSObject, NSCoding {
  var bar: [(string1: String, string2: String)]

  required init?(coder aDecoder: NSCoder) {
    bar = aDecoder.decodeObject(forKey: “bar”) as? [(string1: String, string2: String)] ?? []
  }

  func encode(with aCoder: NSCoder) {
    aCoder.encode(bar, forKey: “bar”) // crash
  }
}

Unfortunately, this code doesn’t seem to work anymore. Is there any way to get a array of tuple encoded without resorting to creating a struct or class in its place?

Thanks,
Saagar Jha

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Philippe Hausler) #3

err sorry mistype it should have read tuples or structs

···

On Sep 22, 2016, at 2:27 PM, Philippe Hausler via swift-users <swift-users@swift.org> wrote:

NSCoding has never worked with either tuples or classes correctly (primarily because it is not really designed to do so). I would suggest to encode and decode either as an array of array of strings and convert or perhaps encode/decode as an array of classes representing the meaning of the tuple.

On Sep 22, 2016, at 2:07 PM, Saagar Jha via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I’ve been working on migrating some old code over to Swift 3, and I’m having some trouble archiving an array of tuples:

class Foo: NSObject, NSCoding {
  var bar: [(string1: String, string2: String)]

  required init?(coder aDecoder: NSCoder) {
    bar = aDecoder.decodeObject(forKey: “bar”) as? [(string1: String, string2: String)] ?? []
  }

  func encode(with aCoder: NSCoder) {
    aCoder.encode(bar, forKey: “bar”) // crash
  }
}

Unfortunately, this code doesn’t seem to work anymore. Is there any way to get a array of tuple encoded without resorting to creating a struct or class in its place?

Thanks,
Saagar Jha

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Saagar Jha) #4

This used to work in a previous beta of Xcode 8 (beta 4, I think…I haven't
checked in the meantime due to issues with Swift 3 migration). I'm guessing
it got changed with the Any/AnyObject work going on in between. I'll go
with using a 2-element array in the meantime.

···

On Thu, Sep 22, 2016 at 14:27 Philippe Hausler <phausler@apple.com> wrote:

err sorry mistype it should have read tuples or structs

On Sep 22, 2016, at 2:27 PM, Philippe Hausler via swift-users < > swift-users@swift.org> wrote:

NSCoding has never worked with either tuples or classes correctly
(primarily because it is not really designed to do so). I would suggest to
encode and decode either as an array of array of strings and convert or
perhaps encode/decode as an array of classes representing the meaning of
the tuple.

On Sep 22, 2016, at 2:07 PM, Saagar Jha via swift-users < > swift-users@swift.org> wrote:

Hello,

I’ve been working on migrating some old code over to Swift 3, and I’m
having some trouble archiving an array of tuples:

class Foo: NSObject, NSCoding {
var bar: [(string1: String, string2: String)]

required init?(coder aDecoder: NSCoder) {
bar = aDecoder.decodeObject(forKey: “bar”) as? [(string1: String, string2:
String)] ?? []
}

func encode(with aCoder: NSCoder) {
aCoder.encode(bar, forKey: “bar”) // crash
}
}

Unfortunately, this code doesn’t seem to work anymore. Is there any way to
get a array of tuple encoded without resorting to creating a struct or
class in its place?

Thanks,
Saagar Jha

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users