Swift 为什么可以用这种奇怪的方式编写UICollectionViewDiffableDataSource初始值设定项?

Swift 为什么可以用这种奇怪的方式编写UICollectionViewDiffableDataSource初始值设定项?,swift,closures,uicollectionviewdiffabledatasource,Swift,Closures,Uicollectionviewdiffabledatasource,在通过保罗·哈德逊学习斯威夫特时,我遇到了一些奇怪的事情 UICollectionViewDiffableDataSource的初始值设定项定义为: public init(collectionView: UICollectionView, cellProvider: @escaping UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider) public

在通过保罗·哈德逊学习斯威夫特时,我遇到了一些奇怪的事情

UICollectionViewDiffableDataSource的初始值设定项定义为:

public init(collectionView: UICollectionView, cellProvider: @escaping UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider)
public init(collectionView:UICollectionView,cellProvider:@escaping UICollectionViewDiffableDataSource.cellProvider)
据我所知,没有其他初始值设定项。但是,Paul成功地将其初始化如下,省去了cellProvider参数:

dataSource = UICollectionViewDiffableDataSource<Section, App>(collectionView: collectionView) { collectionView, indexPath, app in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self, with: app, for: indexPath)
    case "smallTable":
        return self.configure(SmallTableCell.self, with: app, for: indexPath)
    default:
        return self.configure(FeaturedCell.self, with: app, for: indexPath)
    }
}
dataSource=UICollectionViewDiffableDataSource(collectionView:collectionView){collectionView,indexPath,应用程序在
开关self.sections[indexPath.section].type{
案例“中表”:
返回self.configure(MediumTableCell.self,with:app,for:indexPath)
案例“smallTable”:
返回self.configure(SmallTableCell.self,with:app,for:indexPath)
违约:
返回self.configure(FeaturedCell.self,with:app,for:indexPath)
}
}
与此同时,Ray Wenderlich's会这样做:

dataSource = UICollectionViewDiffableDataSource<Section, App>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, app) -> UICollectionViewCell? in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self, with: app, for: indexPath)
    case "smallTable":
        return self.configure(SmallTableCell.self, with: app, for: indexPath)
    default:
        return self.configure(FeaturedCell.self, with: app, for: indexPath)
    }
})
dataSource=UICollectionViewDiffableDataSource(collectionView:collectionView,cellProvider:{(collectionView,indexPath,app)->UICollectionViewCell?在
开关self.sections[indexPath.section].type{
案例“中表”:
返回self.configure(MediumTableCell.self,with:app,for:indexPath)
案例“smallTable”:
返回self.configure(SmallTableCell.self,with:app,for:indexPath)
违约:
返回self.configure(FeaturedCell.self,with:app,for:indexPath)
}
})

我试图理解保罗的方式背后到底是什么样的快速“魔法”,因为他似乎放弃了手机提供商的争论,而是做了一些时髦的了结。他在这里究竟应用了哪些Swift规则?

在您的案例中,您可以看到两种方法,可以使用不同的语法编写相同的规则

在这里,您将以预期的方式使用所有参数,包括两个参数collectionView和cellProvider

dataSource = UICollectionViewDiffableDataSource<Section, App>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, app) -> UICollectionViewCell? in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self, with: app, for: indexPath)
    case "smallTable":
        return self.configure(SmallTableCell.self, with: app, for: indexPath)
    default:
        return self.configure(FeaturedCell.self, with: app, for: indexPath)
    }
})

关于or的更多信息

感谢所有为我指明正确方向的人。查看更多更简明的示例可能会有所帮助:

{ collectionView, indexPath, app in
    switch self.sections[indexPath.section].type {
    case "mediumTable":
        return self.configure(MediumTableCell.self, with: app, for: indexPath)
    case "smallTable":
        return self.configure(SmallTableCell.self, with: app, for: indexPath)
    default:
        return self.configure(FeaturedCell.self, with: app, for: indexPath)
    }
func executeThis(info: String, completion: ((String) -> Void)) {
    completion(info)
}

let completion: ((String) -> Void) = { value in 
    print("Done: " + value)
}

// Pass the closure variable to the completion param:
executeThis(info:"Some Activity", completion:completion)

// Provide the closure directly as an argument:
executeThis(info:"Some Activity 2", completion: { value in
    print("Done: " + value)
})

// Trailing closure: the last param, which is a closure, is implicitly provided via "{ value in"
executeThis(info:"Some Activity 3") { value in
    print("Trailing closure done: " + value)
}

我建议您阅读Swift指南。干得好。还有一件小事,从完成闭包中删除分号。:)
func executeThis(info: String, completion: ((String) -> Void)) {
    completion(info)
}

let completion: ((String) -> Void) = { value in 
    print("Done: " + value)
}

// Pass the closure variable to the completion param:
executeThis(info:"Some Activity", completion:completion)

// Provide the closure directly as an argument:
executeThis(info:"Some Activity 2", completion: { value in
    print("Done: " + value)
})

// Trailing closure: the last param, which is a closure, is implicitly provided via "{ value in"
executeThis(info:"Some Activity 3") { value in
    print("Trailing closure done: " + value)
}