AnyObject – 自分が見落としていた、基本の話

1,601 views

Published on

Swift の AnyObject の性質を、自分はすっかり見落としていたので、それについての基本的なところを確かめておくことにしました。Swift の @objc による動的ディスパッチ、けっこういい感じに出来たんですね。

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,601
On SlideShare
0
From Embeds
0
Number of Embeds
1,281
Actions
Shares
0
Downloads
3
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

AnyObject – 自分が見落としていた、基本の話

  1. 1. #cswift
  2. 2.
  3. 3.
  4. 4. switch instance { case 0 as Double: print("0 of Double") case let value as Int: print("(value) of Int") case is Double: print("Double") }
  5. 5. let object = "Casual" as AnyObject let count: Int? = object.length let _: Void? = object.append?("Swift")
  6. 6. // 通常通りに実行可能(メソッドは IUO 属性付き) let result: String = object.appending("Swift") // 通常は、メソッドに応じられない場合は強制終了 let result: String = object.appending("Swift") // メソッドに ? をつけて、ディスパッチ可能性も判定 let result: String? = object.appending?("Swift")
  7. 7. // 通常通りに実行可能、ただし戻り値は Optional 型 let count: Int? = object.length
  8. 8. class MyClass : NSObject { func myFunction(_ v: Int) -> Int {…} } let object = MyClass() as AnyObject let result: Int? = object.myFunction?(10)
  9. 9. class MyClass : NSObject { func myFunction(_ v: Any) -> NSNumber {…} } let object = MyClass() as NSObject let result: Unmanaged<AnyObject>? = object.perform(MyClass.myFunction(_:), with: 5) let resultValue: Int? = result?.takeRetainedValue().intValue
  10. 10. protocol MyClassProtocol : AnyObject { } class MyClass : NSObject, MyClassProtocol { func myFunction(_ v: Int) -> Int {…} } let object = MyClass() as MyClassProtocol let result: Int? = object.myFunction?(10)
  11. 11. class Object : NSObject { func method1() -> Int {…} @nonobjc func method2() -> Int {…} } let object = Object() as AnyObject let result1 = object.method1?() // .some(x) let result2 = object.method2?() // .none
  12. 12. // 原則、動的ディスパッチが可能 class Object : NSObject { func method1() -> Int {…} // 動的ディスパッチしたくないものに @nonobjc を付与 @nonobjc func method2() -> Int {…} }
  13. 13. // 原則、動的ディスパッチは不可 class Object { func method1() -> Int {…} // 動的ディスパッチしたいものに @objc を付与 @objc func method2() -> Int {…} }
  14. 14. @objc protocol Protocol { func method() } class Object : Protocol { func method() {} }
  15. 15. class Object { @objc(otherName) func originalName() {} } let object = Object() as AnyObject object.originalName?() // .some(()) object.otherName?()
  16. 16. class OtherClass { @objc func otherName() {} } class Object { @objc(otherName) func originalName() {} } let object = Object() as AnyObject object.originalName?() // .some(()) object.otherName?() // .some(())
  17. 17. class OtherClass { @objc func originalName() {} @objc func otherName() {} } class Object { @objc(otherName) func originalName() {} } let object = Object() as AnyObject object.originalName?() // ambiguous use of … object.otherName?() // .some(())
  18. 18. class MyClass : NSObject { func myFunction(_ v: Int) -> Int {…} } let object = MyClass() as AnyObject let result: Int? = object.xxxx?(10)
  19. 19. 
 
 func doSomething() { class Object: NSObject { // このシグネチャーは認識されない。動的実行は可 func someMethod() {} } }

×