简体   繁体   English

Swift:类型XXX必须符合协议'NSObjectProtocol'

[英]Swift : Type XXX must conform to protocol 'NSObjectProtocol'

I am trying to implement a Swift class that must 我正在尝试实现一个必须的Swift类

  1. Inherit from an Objective-C class 从Objective-C类继承
  2. Implement a Objective-C protocol with class variable. 使用类变量实现Objective-C协议。

Although the Objective-C class I am subclassing is inheriting from NSObject, I receive the following compilation error : 虽然我继承的Objective-C类继承自NSObject,但我收到以下编译错误:

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 我继承/实现的Objective-C类和Objective-C协议可以在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. AWSDynamoDBModel有一个很长的继承链,最终从NSObject开始AWSDynamoDBModeling强制执行两个类变量。

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 : Bonus问题:当试图将Objective-C协议强制类变量实现为Swift类变量时,我收到一个编译错误:

Type DDBItem must conform to protocol 'AWSDynamoDBModeling'

Implementing them as function seems to be accepted. 将它们作为函数实现似乎是可以接受的。 Why ? 为什么?

只是继承自NSObject:

class DDBItem : NSObject, AWSDynamoDBModel, AWSDynamoDBModeling {

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 看起来像Swift / Xcode 6.1中的一个错误

Just a heads up for those that stumble upon this post. 对于那些偶然发现这篇文章的人来说,只是一个头脑。 AWSDynamoDBModeling protocol has been changed in the latest SDK (v2.1.1). AWSDynamoDBModeling协议已在最新的SDK(v2.1.1)中进行了更改。 Required functions: dynamoDBTableName and hashKeyAttribute must be static. 必需的函数: dynamoDBTableNamehashKeyAttribute必须是静态的。 The documentation as of today (5/27/2015) appears to be out of date. 截至今日(2015年5月27日)的文件似乎已过时。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM