Swift function containing parameters not available

2020-05-07 05:33发布

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!

2条回答
够拽才男人
2楼-- · 2020-05-07 06:05

The bad boy in your parameter list is the Int? param. Int's are represented as NSInteger in Objective-C. Thus they don't have a pointer and can't have a null value. Consider removing the optional qualifier or changing it to NSNumber?, like: func getSearchResults(searchText: String?, searchType: String?, zoom: Int) -> NSDictionary

查看更多
看我几分像从前
3楼-- · 2020-05-07 06:21

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. Use Int or, if it really can be nil, use NSNumber?.

查看更多
登录 后发表回答