简体   繁体   中英

Block from Objective-C to Swift

I used a framework Objective-C in my Project (Swift). But in the code have a Block, i cannot convert to swift (i'm newbie in swift) So the code is

[self.datePicker setDateHasItemsCallback:^BOOL(NSDate *date) {
    int tmp = (arc4random() % 30)+1;
    return (tmp % 5 == 0);
}];

Please help me. Thank you ,

Where you would use a block in Objective-C, you use a function in Swift. In Objective-C, the argument is a block that takes an NSDate and returns a BOOL:

[self.datePicker setDateHasItemsCallback:^BOOL(NSDate *date) {

So, in Swift the argument is a function that takes an NSDate and returns a Bool:

self.datePicker.setDateHasItemsCallback {
    (date:NSDate) -> Bool in
    return true // fix this up as desired
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM