objective c - swift init not visible in objecitve-C -



objective c - swift init not visible in objecitve-C -

i'm trying create init functions in swift , create instances objective-c. problem don't see in project-swift.h file , i'm not able find function while initializing. have function defined below:

public init(userid: int!) { self.init(style: uitableviewstyle.plain) self.userid = userid }

i tried putting @objc(initwithuserid:) , maintain getting same error again. there else i'm missing? how constructor visible objective-c code?

i read below this:

https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/initialization.html

https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/interactingwithobjective-capis.html

how write init method in swift

how define optional methods in swift protocol?

the issue you're seeing swift can't bridge optional value types -- int value type, int! can't bridged. optional reference types (i.e., class) bridge correctly, since can nil in objective-c. 2 options create parameter non-optional, in case bridged objc int or nsinteger:

// swift public init(userid: int) { self.init(style: uitableviewstyle.plain) self.userid = userid } // objc myclass *instance = [[myclass alloc] initwithuserid: 10];

or utilize optional nsnumber!, since can bridged optional:

// swift public init(userid: nsnumber!) { self.init(style: uitableviewstyle.plain) self.userid = userid?.integervalue } // objc myclass *instance = [[myclass alloc] initwithuserid: @10]; // note @-literal

note, however, you're not treating parameter optional - unless self.userid optional you're setting potential runtime crashes way.

objective-c uitableview swift

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -