[英]Difference between (Type) and Type
Currently, I'm writing a class and protocol named Control
and Controllable
.目前,我正在编写一个名为
Control
和Controllable
的类和协议。
Control
must conform to Controllable
. Control
必须符合Controllable
。
Control
will have an array of other Controllable
s as a stack. Control
将具有其他Controllable
的数组作为堆栈。 Every time before adding Controllable
to the stack, I should check whether the stack contains that element.每次在将
Controllable
添加到堆栈之前,我都应该检查堆栈是否包含该元素。 If yes, move the element to the top of stack.如果是,则将元素移至栈顶。
Okay.好的。 VERSION_1:
VERSION_1:
import Foundation
protocol Controllable: Equatable {
}
func ==<T: Controllable>(lhs: T, rhs: T) -> Bool {
return ObjectIdentifier(lhs).uintValue == ObjectIdentifier(rhs).uintValue
}
class Control: Controllable {
static var mainControl = Control()
private init() {
}
private var stack: [Controllable] = []
func addToStack(controllable: Controllable) {
}
}
There is an error (Xcode 7.3):有一个错误(Xcode 7.3):
return ObjectIdentifier(lhs).uintValue == ObjectIdentifier(rhs).uintValue
Cannot invoke initializer for type 'ObjectIdentifier' with an argument list of type '(T)'
无法使用类型为“(T)”的参数列表调用类型“ObjectIdentifier”的初始值设定项
QUESTION 1: Why?问题1:为什么? How can I construct
ObjectIdentifier
from lhs
and rhs
?如何从
lhs
和rhs
构造ObjectIdentifier
?
VERSION_2: VERSION_2:
import Foundation
protocol Controllable {
}
class Control: Controllable {
static var mainControl = Control()
private init() {
}
private var stack: [Controllable] = []
func addToStack(controllable: Controllable) {
stack.contains({ element in
return ObjectIdentifier(element).uintValue == ObjectIdentifier(controllable).uintValue
})
}
}
There is an error (, again):有一个错误(,再次):
return ObjectIdentifier(element).uintValue == ObjectIdentifier(controllable).uintValue
Cannot invoke initializer for type 'ObjectIdentifier' with an argument list of type '((Controllable))'
无法使用类型为“((Controllable))”的参数列表调用类型“ObjectIdentifier”的初始值设定项
QUESTION 2: Is (Controllable)
tuple?问题 2:是
(Controllable)
元组吗? How should I extract Controllable
from the tuple?我应该如何从元组中提取
Controllable
?
Sorry for my English对不起我的英语不好
ObjectIdentifier only works on objects and metatypes as argument in the constructor. ObjectIdentifier 仅适用于对象和元类型作为构造函数中的参数。
Types that conforms to Controllable are not conforming to the type constraint in the ObjectIdentifier constructor.符合 Controllable 的类型不符合 ObjectIdentifier 构造函数中的类型约束。
init(_ x: AnyObject)
init(_ x: Any.Type)
You want to give the ObjectIdentifier the metatype of a type, like String.self.你想给 ObjectIdentifier 一个类型的元类型,比如 String.self。 Or you want to give the ObjectIdentifier an object, like "hello".
或者你想给 ObjectIdentifier 一个对象,比如“hello”。 You could add the object constraint to the protocol like this:
您可以将对象约束添加到协议中,如下所示:
import Foundation
protocol Controllable: Equatable, AnyObject {
}
func ==<T: Controllable>(lhs: T, rhs: T) -> Bool {
return ObjectIdentifier(lhs).uintValue == ObjectIdentifier(rhs).uintValue
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.