UPDATE: This question does not duplicate the question mentioned above. It was verified (see comment below) that the method definition IS properly added to the Project-Swift.h file. The question pertains to why the method without parameters is available to Obj-C and with parameters it is not.
I have a swift class that is intended to be referenced by an Objective-C class. The Swift class contains a function that will be used to send a network message and return a dictionary containing search results. I'm experiencing odd behavior in that if my function contains parameters, it is not available in the implementing Objective-C class. However, if I remove all the parameters, then the function becomes available.
In my Swift class if I declare the method as follows, it is not available in the Obj-C class and won't compile:
func getSearchResults(searchText:String?, searchType:String?, zoom:Int?) -> NSDictionary
However, removing the parameters like this, the function becomes avaiable to Obj-c and everything works:
func getSearchResults() -> NSDictionary
Why would a function without parameters be available to an Obj-C class while one containing parameters is not? Has anyone seen this kind of behavior? Thanks!
The bad boy in your parameter list is the
Int?
param.Int
's are represented asNSInteger
in Objective-C. Thus they don't have a pointer and can't have anull
value. Consider removing the optional qualifier or changing it toNSNumber?
, like:func getSearchResults(searchText: String?, searchType: String?, zoom: Int) -> NSDictionary
Lou Franco's explanation is correct. Your function has an
Int?
parameter, and there is no equivalent of that in Objective-C. Therefore the entire function cannot be exposed to Objective-C. UseInt
or, if it really can benil
, useNSNumber?
.