Use Objective-C Blocks in Fast

I have a third-party Objective-C library in my quick project, in one of the .h files, it has a typedef :

typedef void (^YDBlutoothToolContectedList) (NSArray *);

and inside the class it has the property:

@property (nonatomic, copy) YDBlutoothToolContectedList blutoothToolContectedList;

(please ignore spelling)

When I try to use this property in my swift class, I use

 bt.blutoothToolContectedList = {(_ tempArray: [Any]) -> Void in self.devices = tempArray self.tableView.reloadData() } 

and I got an error message:

Cannot assign value of type '([Any]) -> Void' to type 'YDBlutoothToolContectedList!'

I know that the above Objective-C code in swift would be the following:

typealias YDBlutoothToolContectedList = () -> Void

but I cannot overwrite this Objective-C file and swift cannot use the closure type, is there a possible way to solve this problem?

+5
source share
1 answer
 typedef void (^YDBlutoothToolContectedList) (NSArray *); 

displayed in Swift as

 public typealias YDBlutoothToolContectedList = ([Any]?) -> Swift.Void 

since the close parameter may be nil . (You can verify that by selecting the .h file and then selecting "Navigation" - "Go to the created interface" in the Xcode menu.)

Therefore, the correct appointment would be

 bt.blutoothToolContectedList = {(_ tempArray: [Any]?) -> Void in // ... } 

or just let the compiler enter the parameter type:

 bt.blutoothToolContectedList = { tmpArray in // ... } 

If you could add a nullification annotation to the Objective-C definition:

 typedef void (^YDBlutoothToolContectedList) (NSArray * _Nonnull ); 

then it will appear in Swift as

 public typealias YDBlutoothToolContectedList = ([Any]) -> Swift.Void 
+10
source

Source: https://habr.com/ru/post/1259392/


All Articles