swift - Can you really call any Objective-C method on AnyObject? -
swift - Can you really call any Objective-C method on AnyObject? -
according "using swift cocoa , objective-c" ibook (near loc 9) regarding id
compatibility:
you can phone call objective-c method , access property without casting more specific class type.
the illustration given in book (shown below) not compile, seems contradict above statement. according book, lastly line below should not execute because length
property not exist on nsdate
object.
var myobject: anyobject = uitableviewcell() myobject = nsdate() allow mylength = myobject.length?
instead, fails compile "could not find overload 'length' accepts supplied arguments" error on lastly line.
is bug, typo, or statement calling objective-c method wrong?
the compiler error saying length
function , phone call wrong arguments
you can myobject.length?()
homecoming nil
also myobject.count?
give nil
without compile error
xcrun swift welcome swift! type :help assistance. 1> import cocoa 2> var obj : anyobject = nsdate() obj: __nsdate = 2014-06-25 13:02:43 nzst 3> obj.length?() $r5: int? = nil 4> obj.count? $r6: int? = nil 5> obj = nsarray() 6> obj.length?() $r8: int? = nil 7> obj.count? $r9: int? = 0 8>
i think compiler doing search method , property called length
, found class provide length()
no class provide length
, hence error message. when phone call [obj nonexistmethod]
in objc, compiler give error saying nonexistmethod
not exist phone call on id
type object
this happened if seek phone call non-exist method in swift
14> obj.notexist? <repl>:14:1: error: 'anyobject' not have fellow member named 'notexist' obj.notexist? ^ ~~~~~~~~ 14> obj.notexist?() <repl>:14:1: error: 'anyobject' not have fellow member named 'notexist()' obj.notexist?() ^ ~~~~~~~~
swift
Comments
Post a Comment