weak引用似乎在Swift中不起作用,除非a protocol声明为@objc,我不想在纯粹的Swift应用程序中.
此代码给出了编译错误(weak不能应用于非类类型MyClassDelegate):
class MyClass {
weak var delegate: MyClassDelegate?
}
protocol MyClassDelegate {
}
Run Code Online (Sandbox Code Playgroud)
我需要为协议添加前缀@objc,然后才能工作.
问题:什么是"纯粹的"Swift方式来实现weak delegate?
在Swift中有可能吗?如果没有,那么有解决方法吗?
我想创建一个可以存储符合特定协议的对象的类.对象应存储在类型化数组中.根据Swift文档协议可以用作类型:
因为它是一种类型,所以您可以在允许其他类型的许多地方使用协议,包括:
- 作为函数,方法或初始值设定项中的参数类型或返回类型
- 作为常量,变量或属性的类型
- 作为数组,字典或其他容器中的项类型
但是,以下生成编译器错误:
协议'SomeProtocol'只能用作通用约束,因为它具有Self或相关类型要求
你怎么解决这个问题:
protocol SomeProtocol: Equatable {
func bla()
}
class SomeClass {
var protocols = [SomeProtocol]()
func addElement(element: SomeProtocol) {
self.protocols.append(element)
}
func removeElement(element: SomeProtocol) {
if let index = find(self.protocols, element) {
self.protocols.removeAtIndex(index)
}
}
}
Run Code Online (Sandbox Code Playgroud) 为什么这个Swift代码没有编译?
protocol P { }
struct S: P { }
let arr:[P] = [ S() ]
extension Array where Element : P {
func test<T>() -> [T] {
return []
}
}
let result : [S] = arr.test()
Run Code Online (Sandbox Code Playgroud)
编译器说:"类型P不符合协议P"(或者,在Swift的更高版本中,"不支持使用'P'作为符合协议'P'的具体类型.").
为什么不?不知怎的,这感觉就像语言中的漏洞.我意识到问题源于将数组声明arr为协议类型的数组,但这是不合理的事情吗?我认为协议正是为了帮助提供类似层次结构的结构?
我试图在Swift中创建一个自定义协议的字典(实际上是一个HashSet),但是它给了我标题中的错误:
协议'myProtocol'只能用作通用约束,因为它具有Self或相关类型要求
我无法做出头脑或尾巴.
protocol Observing: Hashable { }
var observers = HashSet<Observing>()
Run Code Online (Sandbox Code Playgroud) 在Swift中,我可以通过如下声明来明确设置变量的类型:
var object: TYPE_NAME
Run Code Online (Sandbox Code Playgroud)
如果我们想更进一步并声明一个符合多个协议的变量,我们可以使用protocol声明:
var object: protocol<ProtocolOne,ProtocolTwo>//etc
Run Code Online (Sandbox Code Playgroud)
如果我想声明一个符合一个或多个协议并且也是特定基类类型的对象,该怎么办?Objective-C等价物如下所示:
NSSomething<ABCProtocolOne,ABCProtocolTwo> * object = ...;
Run Code Online (Sandbox Code Playgroud)
在Swift中,我希望它看起来像这样:
var object: TYPE_NAME,ProtocolOne//etc
Run Code Online (Sandbox Code Playgroud)
这使我们能够灵活地处理基类型的实现以及协议中定义的添加接口.
还有另一种我可能会失踪的更明显的方式吗?
举个例子,假设我有一个UITableViewCell负责返回符合协议的单元的工厂.我们可以轻松设置一个返回符合协议的单元格的泛型函数:
class CellFactory {
class func createCellForItem<T: UITableViewCell where T:MyProtocol >(item: SpecialItem,tableView: UITableView) -> T {
//etc
}
}
Run Code Online (Sandbox Code Playgroud)
后来我想在利用类型和协议的同时将这些单元格出列
var cell: MyProtocol = CellFactory.createCellForItem(somethingAtIndexPath) as UITableViewCell
Run Code Online (Sandbox Code Playgroud)
这会返回错误,因为表视图单元格不符合协议...
我希望能够指定单元格是否UITableViewCell符合MyProtocol变量声明?
如果您熟悉工厂模式,那么在能够返回实现特定接口的特定类的对象的上下文中这将是有意义的.
就像在我的例子中一样,有时我们喜欢定义在应用于特定对象时有意义的接口.我的表格视图单元格的例子就是这样一个理由.
虽然提供的类型并不完全符合上面提到的接口,但是工厂返回的对象也是如此,因此我希望能够灵活地与基类类型和声明的协议接口进行交互
我已经声明了一个Swift协议:
protocol Option {
var name: String { get }
}
Run Code Online (Sandbox Code Playgroud)
我声明了这个协议的多个实现 - 一些类,一些枚举.
我有一个视图控制器,其属性声明如下:
var options: [Option] = []
Run Code Online (Sandbox Code Playgroud)
当我尝试将此属性设置为Option在另一个VC 中实现协议的对象数组时prepareForSegue,我收到运行时错误:
fatal error: array cannot be bridged from Objective-C
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?编译器拥有它需要的所有信息,而且我不明白Objective-C与它有什么关系 - 我的项目只包含Swift文件,并且这些数组不会进入或退出任何框架方法需要他们被桥接到NSArray.
Swift文档说类,结构和枚举都可以符合协议,我可以达到一致的程度.但我无法让enum表现得像类和结构示例:
protocol ExampleProtocol {
var simpleDescription: String { get set }
mutating func adjust()
}
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
}
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
mutating func adjust() {
simpleDescription …Run Code Online (Sandbox Code Playgroud) Non-'@objc' method 'presentationController(_:viewControllerForAdaptivePresentationStyle:)' does not satisfy optional requirement of '@objc' protocol 'UIAdaptivePresentationControllerDelegate'
Run Code Online (Sandbox Code Playgroud)
@objc但没有帮助@objc protocol P1 : UIAdaptivePresentationControllerDelegate {
}
extension P1 where Self : UIViewController {
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
return UIViewController()
}
}
class A : UIViewController, P1 {
}
Run Code Online (Sandbox Code Playgroud) 是否有一种在Swift中制作"纯虚函数"的标准方法,即.一个必须被每个子类覆盖的,如果不是,会导致编译时错误?
swift ×10
swift-protocols ×10
ios ×4
generics ×3
abstract ×1
delegates ×1
enums ×1
objective-c ×1
swift3 ×1
xcode6 ×1