包含在 Objective-C 类中不可用的参数的 Swift 函数 [英] Swift function containing parameters not available in Objective-C class

查看:53
本文介绍了包含在 Objective-C 类中不可用的参数的 Swift 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这个问题不重复上面提到的问题.已验证(见下面的评论)方法定义是正确添加到 Project-Swift.h 文件中.问题涉及为什么没有参数的方法可用于 Obj-C 并且带有参数不是.

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.

我有一个 swift 类,旨在由 Objective-C 类引用.Swift 类包含一个函数,该函数将用于发送网络消息并返回包含搜索结果的字典.我遇到了奇怪的行为,因为如果我的函数包含参数,则它在实现的 Objective-C 类中不可用.但是,如果我删除所有参数,则该功能可用.

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.

在我的 Swift 类中,如果我按如下方式声明该方法,则它在 Obj-C 类中不可用且无法编译:

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

然而,删除这样的参数,该函数变得可用于 Obj-c 并且一切正常:

However, removing the parameters like this, the function becomes avaiable to Obj-c and everything works:

func getSearchResults() -> NSDictionary

为什么 Obj-C 类可以使用没有参数的函数,而包含参数的函数却不能?有没有人见过这种行为?谢谢!

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!

推荐答案

参数列表中的坏孩子是 Int? 参数.Int 在 Objective-C 中表示为 NSInteger.因此它们没有指针并且不能有 null 值.考虑删除可选的限定符或将其更改为 NSNumber?,如:func getSearchResults(searchText: String?, searchType: String?, zoom: Int) ->NSD字典

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

这篇关于包含在 Objective-C 类中不可用的参数的 Swift 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆