I have a third party Objective-C library in my swift project, in one of the .h files, it has a typedef
:
我在swift项目中有一个第三方Objective-C库,在.h文件中,它有一个typedef:
typedef void (^YDBlutoothToolContectedList) (NSArray *);
typedef void(^ YDBlutoothToolContectedList)(NSArray *);
and inside the class, it has a property:
在课堂内,它有一个属性:
@property (nonatomic, copy) YDBlutoothToolContectedList blutoothToolContectedList;
@property(非原子,复制)YDBlutoothToolContectedList blutoothToolContectedList;
(please ignore its spelling)
(请忽略其拼写)
When I try to use this property in my swift class, I use
当我尝试在我的swift类中使用这个属性时,我会使用
bt.blutoothToolContectedList = {(_ tempArray: [Any]) -> Void in
self.devices = tempArray
self.tableView.reloadData()
}
and I got the error says:
我得到错误说:
Cannot assign value of type '([Any]) -> Void' to type 'YDBlutoothToolContectedList!'
无法指定类型'([Any]) - > Void'的值来输入'YDBlutoothToolContectedList!'
I know the above Objective-C code in swift would be:
我知道swift中的上述Objective-C代码是:
typealias YDBlutoothToolContectedList = () -> Void
typealias YDBlutoothToolContectedList =() - > Void
but I can't re-write that Objective-C file and swift can't cast the closure type, is there a possible way to solve this problem?
但是我不能重写那个Objective-C文件和swift无法转换关闭类型,有没有可能解决这个问题的方法呢?
1 个解决方案
#1
11
typedef void (^YDBlutoothToolContectedList) (NSArray *);
is mapped to Swift as
映射到Swift as
public typealias YDBlutoothToolContectedList = ([Any]?) -> Swift.Void
because the closure parameter can be nil
. (You can verify that by selecting the .h-file and then choosing Navigate->Jump to Generated Interface in the Xcode menu.)
因为闭包参数可以是零。 (您可以通过选择.h文件,然后在Xcode菜单中选择Navigate-> Jump to Generated Interface来验证。)
Therefore the correct assignment would be
因此,正确的任务将是
bt.blutoothToolContectedList = {(_ tempArray: [Any]?) -> Void in
// ...
}
or simply let the compiler infer the parameter type:
或者只是让编译器推断出参数类型:
bt.blutoothToolContectedList = { tmpArray in
// ...
}
If you could add a nullability annotation to the Objective-C definition:
如果可以为Objective-C定义添加可空性注释:
typedef void (^YDBlutoothToolContectedList) (NSArray * _Nonnull );
then it would be mapped to Swift as
那么它将被映射到Swift as
public typealias YDBlutoothToolContectedList = ([Any]) -> Swift.Void
#1
11
typedef void (^YDBlutoothToolContectedList) (NSArray *);
is mapped to Swift as
映射到Swift as
public typealias YDBlutoothToolContectedList = ([Any]?) -> Swift.Void
because the closure parameter can be nil
. (You can verify that by selecting the .h-file and then choosing Navigate->Jump to Generated Interface in the Xcode menu.)
因为闭包参数可以是零。 (您可以通过选择.h文件,然后在Xcode菜单中选择Navigate-> Jump to Generated Interface来验证。)
Therefore the correct assignment would be
因此,正确的任务将是
bt.blutoothToolContectedList = {(_ tempArray: [Any]?) -> Void in
// ...
}
or simply let the compiler infer the parameter type:
或者只是让编译器推断出参数类型:
bt.blutoothToolContectedList = { tmpArray in
// ...
}
If you could add a nullability annotation to the Objective-C definition:
如果可以为Objective-C定义添加可空性注释:
typedef void (^YDBlutoothToolContectedList) (NSArray * _Nonnull );
then it would be mapped to Swift as
那么它将被映射到Swift as
public typealias YDBlutoothToolContectedList = ([Any]) -> Swift.Void