ios - How to pass swift enum with @objc tag -
i need define protocol can called in class use objective-c type
but doing doesn't work:
enum newscellactiontype: int { case vote = 0 case comments case time } @objc protocol newscelldelegate { func newscelldidselectbutton(cell: newscell, actiontype: newscellactiontype) }
you error
swift enums cannot represented in objective-c
if don't put @objc tag on protocol it'll crash app it's called in class adopt protocol , inherit objective-c type class (like uiviewcontroller).
so question is, how should declare , pass enum @objc tag?
swift enums different obj-c (or c) enums , can't passed directly obj-c.
as workaround, can declare method int
parameter.
func newscelldidselectbutton(cell: newscell, actiontype: int)
and pass newscellactiontype.vote.toraw()
. won't able access enum names obj-c though , makes code more difficult.
a better solution might implement enum in obj-c (for example, in briding header) because automatically accessible in swift , possible pass parameter.
edit
it not required add @objc
use obj-c class. if code pure swift, can use enums without problems, see following example proof:
enum newscellactiontype : int { case vote = 0 case comments case time } protocol newscelldelegate { func newscelldidselectbutton(cell: uitableviewcell?, actiontype: newscellactiontype ) } @uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate, newscelldelegate { var window: uiwindow? func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: nsdictionary?) -> bool { self.window = uiwindow(frame: uiscreen.mainscreen().bounds) self.window!.backgroundcolor = uicolor.whitecolor() self.window!.makekeyandvisible() test() return true; } func newscelldidselectbutton(cell: uitableviewcell?, actiontype: newscellactiontype) { println(actiontype.toraw()); } func test() { self.newscelldidselectbutton(nil, actiontype: newscellactiontype.vote) } }
Comments
Post a Comment