ios Swift : 类型 XXX 必须符合协议“NSObjectProtocol”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26814829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift : Type XXX must conform to protocol 'NSObjectProtocol'
提问by Sébastien Stormacq
I am trying to implement a Swift class that must
我正在尝试实现一个必须的 Swift 类
- Inherit from an Objective-C class
- Implement a Objective-C protocol with class variable.
- 从 Objective-C 类继承
- 使用类变量实现 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'
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 :
额外问题:当尝试将 Objective-C 协议强制的类变量实现为 Swift 类变量时,我收到一个编译错误:
Type DDBItem must conform to protocol 'AWSDynamoDBModeling'
Type DDBItem must conform to protocol 'AWSDynamoDBModeling'
Implementing them as function seems to be accepted. Why ?
将它们实现为函数似乎是可以接受的。为什么 ?
采纳答案by Sébastien Stormacq
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 中的一个错误
回答by Johnny Z
Just inherit from NSObject:
只需从 NSObject 继承:
class DDBItem : NSObject, AWSDynamoDBModel, AWSDynamoDBModeling {
回答by Nick
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.
只是提醒那些偶然发现这篇文章的人。AWSDynamoDBModeling
协议已在最新的 SDK (v2.1.1) 中更改。必需的功能:
dynamoDBTableName
并且hashKeyAttribute
必须是静态的。截至今天(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)
}
}
回答by Jon Vogel
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)
}