Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : Type XXX must conform to protocol 'NSObjectProtocol'

I am trying to implement a Swift class that must

  1. Inherit from an Objective-C class
  2. Implement a Objective-C protocol with class variable.

Although the Objective-C class I am subclassing is inheriting from NSObject, I receive the following compilation error :

Type DDBItem must conform to protocol 'NSObjectProtocol'

The Objective-C class and Objective-C protocol I am inheriting / implementing are available at https://github.com/aws/aws-sdk-ios/blob/master/DynamoDB/AWSDynamoDBObjectMapper.h

AWSDynamoDBModel has a long chain of inheritance that eventually starts with NSObject AWSDynamoDBModeling is enforcing two class variables.

My code is

class DDBItem : AWSDynamoDBModel, AWSDynamoDBModeling {  //    class var dynamoDBTableName : String { get { return "" }} //    class var hashKeyAttribute  : String { get { return "" }}      class func dynamoDBTableName() -> String! {         return ""     }     class func hashKeyAttribute() -> String! {         return ""     } } 

Bonus Question : when trying to implement the Objective-C protocol mandated class variables as Swift class variables, I receive a compilation error :

Type DDBItem must conform to protocol 'AWSDynamoDBModeling'

Implementing them as function seems to be accepted. Why ?

like image 553
Sébastien Stormacq Avatar asked Nov 08 '14 07:11

Sébastien Stormacq


2 Answers

Just inherit from NSObject:

class DDBItem : NSObject, AWSDynamoDBModel, AWSDynamoDBModeling { 
like image 172
Johnny Z Avatar answered Sep 22 '22 01:09

Johnny Z


Self answered for sake of archiving.

When adding

override func isEqual(anObject: AnyObject?) -> Bool {     return super.isEqual(anObject) } 

to my class, it works. This method should have been inherited from the base class.

Looks like a bug in Swift / Xcode 6.1 to me

like image 21
Sébastien Stormacq Avatar answered Sep 20 '22 01:09

Sébastien Stormacq