ios - what does `@objc(ViewController) ` do? -


i have swift code:

func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject : anyobject]?) -> bool { ...     let storyboard = uistoryboard(name: "main", bundle: nil)         let controller = storyboard.instantiateinitialviewcontroller()!         if (controller inlinemainviewcontroller ){             mainviewcontroller = controller as! inlinemainviewcontroller         }         window?.rootviewcontroller = controller         window?.makekeyandvisible() } 

when viewcontroller has attribute:

@objc(viewcontroller)  // match objc symbol name inside storyboard public class inlinemainviewcontroller: uiviewcontroller,  

(controller inlinemainviewcontroller ) returns false

and when doesn't have attribute:

public class inlinemainviewcontroller: uiviewcontroller,  

(controller inlinemainviewcontroller ) returns true

my main.sotryboard connected inlinemainviewcontroller

i took @objc(viewcontroller) google analytics github example

from swift type compatibility

you can use @objc attribute if swift class doesn’t inherit objective-c class, or if want change name of symbol in interface it’s exposed objective-c code.

this code:

@objc(viewcontroller) 

will cause class following attribute have name in parentheses when imported objective-c code through generated header, discussed in importing swift objective-c

to import swift code objective-c same target

  • import swift code target objective-c .m file within target using syntax , substituting appropriate name:

    #import "productmodulename-swift.h"


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -