0
内下面的代码不编译对我来说:访问迅速类扩展方法从原始类
import Foundation
@objc public protocol MyProtocol {
func protocolMethod(parameter: (String)) -> String
func anotherProtocolMethod() -> Int
}
class MyClass: NSObject {
var myValue = self.anotherProtocolMethod()
}
extension MyClass: MyProtocol {
func protocolMethod(parameter: (String)) -> String {
return ""
}
func anotherProtocolMethod() -> Int {
return 1
}
}
上myvalue的被分配的线路中发生的错误,它读取: 错误:类型NSObject的的“值 - >() - > MyClass没有成员anotherProtocolMethod
有没有办法让这个工作没有移动myValue到类扩展?
一个好的解决方案是声明属性'lazy'。查看关联问题的答案。 – matt
或使用计算的属性而不是存储的属性。请参阅我的书进一步讨论:http://www.apeth.com/swiftBook/ch04.html#_properties – matt
感谢您的答案。计算只读属性对我的用例非常有用。 –