1

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!

3
  • 2
    1. Try it without the Int? parameter -- maybe it can't translate that. 2. Check your Project-Swift.h file (the generated header -- replace Project with your project's name) -- see if getSearchResults is there and how it is declared -- maybe it's just not something you expected. Commented May 17, 2016 at 18:14
  • I checked the Project-Swift.h file and the method is properly declared: - (NSDictionary * _Nonnull)getSearchResults:(id _Nonnull)searchText searchType:(id _Nonnull)searchType zoom:(NSInteger)zoom; Commented May 17, 2016 at 18:21
  • 2
    @Pheepster: I have double-checked it. - (NSDictionary * _Nonnull)getSearchResults:(NSString * _Nullable)searchText searchType:(NSString * _Nullable)searchType zoom:(NSInteger)zoom; is declared in Project-Swift.h only if you make the last parameter non-optional zoom: Int (or if you remove that parameter). Commented May 17, 2016 at 19:30

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This does make sense, however, even this won't compile: func getSearchResults(searchText:NSString) -> NSDictionary
Seems that having any parameter at all causes it to be unavailable
2

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?.

1 Comment

And I would advise using String or NSString? instead of String? as well, because Swift 3 is going to be even stricter about this sort of thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.