Swift任何方法参数Protocol Conformance Lost

时间:2017-11-15 16:43:45

标签: swift swift-protocols

def qw():
    global en1
    if en1.get() == 2e2c2v:
        print("You have visa to Paris")
        tp.destroy()  # after printing should close the toplevel window
    else:
        print("you dont have access to print the data")


def login_toplevel():
    global en1
    tp = Toplevel()
    tp.geometry("300x300")
    en1 = Entry(tp)
    en1.pack()
    b = Button(tp, text="provide key to print", command=qw)
    b.pack()


root = Tk()
root.geometry("400x400")

b = Button(root, text= "print", command=login_toplevel).pack()

root.mainloop()

我希望这会是protocol A { } class B: A { } func f(x: Any) { print(x is A) } let x: B? = B() f(x: x) // false 。这是Swift中的错误吗?

以下示例正常工作并返回true

true

1 个答案:

答案 0 :(得分:0)

如果您想测试您的实例是否符合协议A,您可以使用:

func<T>(x: T) {
   print(x is A)
}

或者同时使用这两个功能:

func(x: Any) {
   print(x is A)
}

func(x: Optional<Any>) {
   print(x is A)
}