求解块形式 objective-c 到 swift 3

solve block form objective-c to swift 3

我在块上遇到问题,在 objective-c 上它运行良好,但我无法转换为 swift 3,

在 objective-c 案例中

typedef void (^PanCellToDeleteBlock)(NSIndexPath *cellIndexPath);

@interface MyCollectionViewCell : UICollectionViewCell<UIGestureRecognizerDelegate>

@property (copy , nonatomic) PanCellToDeleteBlock panCellToDeleteBlock;

在swift 3 例

public typealias PanCellToDeleteBlock = (_ cell:NSIndexPath) -> Void

class MyCollectionViewCell: UICollectionViewCell,UIGestureRecognizerDelegate {

let panCellToDeleteBlock:PanCellToDeleteBlock? = nil

看起来没有swift.......

这是我在同一个地方使用它的时候 class

在 objective-c 案例中

if (_panCellToDeleteBlock) {
    _panCellToDeleteBlock(_cellIndexPath);
}

在swift情况下,一切看起来都不对

if (self.panCellToDeleteBlock != nil) {
    self.panCellToDeleteBlock(self.cellIndexPath)
}

这是必须疯狂的部分,这是在 viweController 中 OC案例

cell.panCellToDeleteBlock = ^(NSIndexPath *cellIndexPath){
    [weakSelf.dataArray removeObjectAtIndex:cellIndexPath.row];
    [weakSelf.collectionView reloadData];
};

swift部分我已经无能为力了....

你试过这个吗

public typealias PanCellToDeleteBlock = (_ indexPath:NSIndexPath) -> Void

cell.panCellToDeleteBlock: PanCellToDeleteBlock = { [weak self] indexPath in
    self?.dataArray.remove(at: indexPath.row)
    self?.collectionView.reloadData()
}

Swift 3 个等价物是

public typealias PanCellToDeleteBlock = (IndexPath) -> Void

class MyCollectionViewCell: UICollectionViewCell,UIGestureRecognizerDelegate {

var panCellToDeleteBlock : PanCellToDeleteBlock?

self.panCellToDeleteBlock?(self.cellIndexPath)

cell.panCellToDeleteBlock = { [weak self] indexPath in
    self?.dataArray.remove(at: indexPath.row)
    self?.collectionView.reloadData()
}