Swift and NSCoding: Encoding an array of objects conforming to a class-only protocol

tktsubota

I have a class, StandardObject, that conforms to the protocol Object. Another class, ObjectManager, has a property called objects which is an array containing instances of Object. Both StandardObject and ObjectManager are subclasses of NSObject and conform to NSCoding.

When I try to encode the objects property of ObjectManager in encodeWithCoder:, I receive an error:

cannot convert value of type '[Object]' to expected argument type 'AnyObject?'

Here is my code:

ObjectManager:

class ObjectManager: NSObject, NSCoding {

    var objects = [Object]()

    required init?(coder aDecoder: NSCoder) {

    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(objects, forKey: "Objects") //// ERROR HERE
    }

}

Object protocol:

protocol Object: class, NSCoding {
    // Currently completely empty
}

StandardObject:

class StandardObject: NSObject, Object {

    required init?(coder aDecoder: NSCoder) {

    }

    func encodeWithCoder(aCoder: NSCoder) {

    }

}

I know it has to do with the fact that you can only encode objects (or arrays of them), not structs, enums, or in this case, protocols. However, the Object protocol's declaration is:

protocol Object: class, NSCoding

which means that only classes can conform to this protocol.

Shouldn't that mean that there are only instances of classes in the objects array? Why can't it be encoded?

I have also tried casting the array to an NSArray before encoding, but I get this error:

cannot convert value of type '[Object]' to type 'NSArray' in coercion

So here are my two questions:

  1. If I made the Object protocol only have objects conform to it, shouldn't an array of Object be an array of objects?
  2. If question 1 isn't possible for some reason, how do you convert the array of Object to be able to be encoded with NSCoding?
Cristik

You need to declare your protocol as @objc, to place it into the Objective-C world:

@objc protocol Object: class, NSCoding {

The compiler will then know he will be able to toll-free bridge the Swift array with a NSArray, as you'll be able to build-up the array only with instances of classes derived from NSObject.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift Class Extension Only When Conforming to Protocol

From Dev

How to create an array of objects of a specific type, conforming to a protocol in Swift 2

From Dev

Declaring Class conforming to protocol in Swift

From Dev

Swift Collections With Class Conforming to Protocol

From Dev

ios swift class conforming protocol

From Dev

Swift class extension with protocol vs conforming to protocol

From Dev

How to define an array of objects conforming to a protocol?

From Dev

Swift class conforming to Objective-C protocol

From Java

Class conforming to protocol as function parameter in Swift

From Dev

Swift class conforming to protocol, how to pass it as a parameter?

From Dev

Swift array of struct types conforming to protocol

From Dev

Extending typed array by conforming to a protocol in Swift 2

From Dev

Swift array of struct types conforming to protocol

From Dev

Swift Check if Two Objects Conforming to a Protocol Are Referentially The Same

From Dev

Swift Check if Two Objects Conforming to a Protocol Are Referentially The Same

From Dev

Can't create an Array of types conforming to a Protocol in Swift

From Dev

Detect class of an object conforming to a protocol?

From Dev

Detect class of an object conforming to a protocol?

From Dev

Swift: Does not conform to protocol NSCoding

From Dev

NSCoding encode Array of Objects

From Dev

NSCoding encode Array of Objects

From Dev

Swift protocol extension with property conforming to protocol

From Dev

Swift: Array property with elements conforming to a class and multiple protocols simultaneously

From Dev

Can we test if objects conforming to the same protocol are identical in swift without casting?

From Dev

Swift cannot pass class conforming to protocol as function parameter to a function residing in Objective-C file

From Dev

Swift 2 - Protocol conforming to Equatable issue

From Dev

Swift Struct with Lazy, private property conforming to Protocol

From Dev

How to list all classes conforming to protocol in Swift?

From Dev

Swift: conforming to protocol using extension and composition pattern

Related Related

  1. 1

    Swift Class Extension Only When Conforming to Protocol

  2. 2

    How to create an array of objects of a specific type, conforming to a protocol in Swift 2

  3. 3

    Declaring Class conforming to protocol in Swift

  4. 4

    Swift Collections With Class Conforming to Protocol

  5. 5

    ios swift class conforming protocol

  6. 6

    Swift class extension with protocol vs conforming to protocol

  7. 7

    How to define an array of objects conforming to a protocol?

  8. 8

    Swift class conforming to Objective-C protocol

  9. 9

    Class conforming to protocol as function parameter in Swift

  10. 10

    Swift class conforming to protocol, how to pass it as a parameter?

  11. 11

    Swift array of struct types conforming to protocol

  12. 12

    Extending typed array by conforming to a protocol in Swift 2

  13. 13

    Swift array of struct types conforming to protocol

  14. 14

    Swift Check if Two Objects Conforming to a Protocol Are Referentially The Same

  15. 15

    Swift Check if Two Objects Conforming to a Protocol Are Referentially The Same

  16. 16

    Can't create an Array of types conforming to a Protocol in Swift

  17. 17

    Detect class of an object conforming to a protocol?

  18. 18

    Detect class of an object conforming to a protocol?

  19. 19

    Swift: Does not conform to protocol NSCoding

  20. 20

    NSCoding encode Array of Objects

  21. 21

    NSCoding encode Array of Objects

  22. 22

    Swift protocol extension with property conforming to protocol

  23. 23

    Swift: Array property with elements conforming to a class and multiple protocols simultaneously

  24. 24

    Can we test if objects conforming to the same protocol are identical in swift without casting?

  25. 25

    Swift cannot pass class conforming to protocol as function parameter to a function residing in Objective-C file

  26. 26

    Swift 2 - Protocol conforming to Equatable issue

  27. 27

    Swift Struct with Lazy, private property conforming to Protocol

  28. 28

    How to list all classes conforming to protocol in Swift?

  29. 29

    Swift: conforming to protocol using extension and composition pattern

HotTag

Archive