- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
研究如何在 Swift 3 中构建类似的东西。
我习惯在 Unity3d 中使用这种架构,也许没有类似的东西来处理异步数据。我听说过完成 block +使用协议(protocol)/委托(delegate)在 Swift 中传递数据,但我认为拥有一个获取数据的公共(public)函数然后使用协程等待所有数据都在 VC 中会更容易开始吧。
这是我用来创建它们的 C# 代码:
List<Int> data = new List<Int>;
private IENumerator get_data()
{
run_async_func_to_get_data();
while(data.count == 0)
{
yield return null;
}
yield break;
}
private IENumerator start_game()
{
yield return get_data();
yield return use_data();
}
void Start()
{
StartCoroutine(start_game);
}
最佳答案
不,Swift 目前不支持 Unity/C# style coroutines (即,yield return
样式构造)。顺便说一下,这样的功能非常酷 ;-)
话虽如此,您可能想看看 Async framework对于一个有点有效的替代方案(如果你真的在寻找异步/等待抽象):
Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
至于 Swift 原生支持,我们可能需要 wait for Swift ≥ 5对于类似的事情:
Actors, async/await, atomicity, memory model, and related topics. This area is highly desired by everyone, as it will open the door for all sorts of new things on the client, server and more. We plan to start formal discussions about this in Phase 2, but it is unfortunately crystal clear that a new concurrency model won’t be done in time for the Swift 4 release. This is simply because it will take more than a 12 months to design and build, and we want to make sure to take time to do it right. It also makes sense for the memory ownership model to be better understood before taking this on.
关于Swift 等同于 Unity3d 协程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43505101/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!