ios - Downcast from Any to a protocol -
i have next code. how can resolve error in lastly line?
protocol animal { func walk() } struct cat: animal { func walk() {} init() { } } var obj: = cat() var cat = obj animal // error: cannot downcast unrelated type animal
update: has been fixed in swift 1.2+ (xcode 6.3+). xcode 6.3 beta release notes say:
dynamic casts (“as!", “as?" , “is”) work swift protocol types, long have no associated types.
you can check protocol conformance (which includes is
, as
, , as?
) @objc
protocol. animal
not @objc
.
see checking protocol conformance section of swift book.
note
you can check protocol conformance if protocol marked @objc attribute
ios swift
No comments:
Post a Comment