首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么可以用这种奇怪的方式编写UICollectionViewDiffableDataSource初始化器?

为什么可以用这种奇怪的方式编写UICollectionViewDiffableDataSource初始化器?
EN

Stack Overflow用户
提问于 2021-03-08 18:02:35
回答 2查看 58关注 0票数 1

在通过Paul Hudson的tutorial学习Swift时,我遇到了一些奇怪的事情。

UICollectionViewDiffableDataSource的初始化器定义为:

代码语言:javascript
复制
public init(collectionView: UICollectionView, cellProvider: @escaping UICollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType>.CellProvider)

据我所知,没有其他的初始化器。但是,Paul成功地将其初始化为这样,省略了cellProvider参数:

代码语言:javascript
复制
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)
    }
}

同时,雷·温德利希的tutorial会这样做:

代码语言:javascript
复制
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)
    }
})

我试图理解保罗的方式背后发生了什么样的斯威夫特“魔法”,因为他似乎放弃了cellProvider的争论,而是做了一些时髦的闭包事情。他在这里具体应用了哪些Swift规则?

EN

回答 2

Stack Overflow用户

发布于 2021-03-08 19:22:29

在您的例子中,您可以看到两种方法如何使用不同的语法编写相同的代码。

在这里,您通过两个参数collectionView和cellProvider以预期的方式使用所有参数。

代码语言:javascript
复制
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)
    }
})

在这里,您使用第二个参数(cellProvider:)作为括号后面的闭包,除了最后一个参数。

代码语言:javascript
复制
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)
    }
}

请注意,这两个示例都是相同的:

代码语言:javascript
复制
{ 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)
    }

有关Trailing ClosuresWhat is trailing closure syntax?的更多信息

票数 1
EN

Stack Overflow用户

发布于 2021-03-08 20:23:45

感谢为我指明正确方向的每一个人。查看其他更精简的示例可能会有所帮助:

代码语言:javascript
复制
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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66527770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档