2016-11-20 68 views
4

在Swift 3中,通知与NSNotification之间有什么区别?Swift 3:NS通知与通知

具体而言,在Notification结构中,存在NSNotification的ReferenceType typealias。这里使用ReferenceType的方式是什么?

public struct Notification : ReferenceConvertible, Equatable, Hashable { 

    public typealias ReferenceType = NSNotification 
    ... 
} 

回答

0

Notification是一个围绕NSNotification的结构包装。您可以桥接使用as,这是ReferenceConvertible所做的。

+0

由于它是一个包装,为什么我不能直接使用NSNotification?你能解释一下ReferenceConvertible是如何工作的吗? – Boon

+0

为什么你期望能够直接使用它,因为它是一个包装?如果我没有记错,ReferenceConvertible就是编译器的魔力。 – Alexander

+0

@Boon'ReferenceType'是Swift值类型的装饰,它由Foundation引用类型支持。正如亚历山大所说,它被编译器用于在NSNotification和Notification之间进行投射。我们不需要担心,这是一个内部机制。 – Moritz