32
votes

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 ?

4
For the second question, property is just syntax sugar, when do bridging, I guess the compiler did the syntax sugar mapping for OC property to make them like property in Swift. BUT there is no class level property in OC, and the +() is a method, which the compiler just maps it to a swift class level method. It can only be called with A.classmethod(). - Shuo
For the first question, I have no clue on what is happened. Even you inherit from MTLModel which directly inherit from NSObject, the error still exists. Which means the deep nest is not the problem, but the MLTModel implementation. - Shuo
Good point, I will have a look at MLTModel implementation - Sébastien Stormacq
mmmh, inheriting directly from MTLModel works ... class DDBItem : MTLModel - Sébastien Stormacq
but as soon as I am adding the protocol, the compilation error kicks in (Type must conform to NSObjectProtocol) even with "class DDBItem : NSOBject, AWSDynamoDBModeling {" - Sébastien Stormacq

4 Answers

78
votes

Just inherit from NSObject:

class DDBItem : NSObject, AWSDynamoDBModel, AWSDynamoDBModeling {
10
votes

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

1
votes

Just a heads up for those that stumble upon this post. AWSDynamoDBModeling protocol has been changed in the latest SDK (v2.1.1). Required functions: dynamoDBTableName and hashKeyAttribute must be static. The documentation as of today (5/27/2015) appears to be out of date.

Example:

class Dingle:AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    static func dynamoDBTableName() -> String! {
        return "dev_coupons"
    }

    static func hashKeyAttribute() -> String! {
        return "status "
    }

    func rangeKeyAttribute() -> String! {
        return "post_date"
    }

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

Confirmed! Write the functions this way:

static func dynamoDBTableName() -> String {


    return "pb_Test"

}


static func hashKeyAttribute() -> String {



    return "ID"
}

And you have to include this:

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