Swift相当于Unity3d协程吗? [英] Swift equivalent of Unity3d Coroutines?

查看:114
本文介绍了Swift相当于Unity3d协程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找如何在Swift 3中构建类似的东西.

Looking how to build something similar in Swift 3.

我习惯在Unity3d中使用这种架构,也许没有类似的东西可以处理异步数据.我听说过完成块+使用协议/代理在Swift中传递数据,但是我认为拥有一个公共函数来获取数据会更容易,然后使用协程等待所有数据都在VC中开始吧.

I'm used to using this sort of architecture in Unity3d, perhaps there isn't anything similar to handle async data. I've heard of completion blocks + using protocols/delegates to pass data in Swift but I thought it'd be easier to have a public function that gets the data and then use a coroutine to wait until all the data is in the VC to start things up.

这是我用来创建它们的C#代码:

Here is C# code I'd use to create them:

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当前不支持

No, Swift do not currently support Unity/C# style coroutines (i.e., yield return style constructs). Such feature is pretty cool by the way ;-)

话虽如此,您可能想看一下异步框架,以了解某种有效的方法另一种选择(如果您确实正在寻找异步/等待抽象):

Having said that, you might want to take a look in the Async framework for a somewhat valid alternative (if you are really looking for async/await abstractions):

Swift中的语法糖用于Grand Central Dispatch中的异步调度

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch

对于Swift本机支持,我们可能需要

As for Swift native support, we might need to wait for Swift ≥ 5 for something like that to come along:

演员,异步/等待,原子性,内存模型和相关主题.每个人都非常希望这个领域,因为它将为客户端,服务器等上的各种新事物打开大门.我们计划在第二阶段开始对此进行正式讨论,但是很遗憾,很明显,Swift 4版本不会及时完成新的并发模型.这仅仅是因为设计和构建将花费超过12个月的时间,并且我们希望确保花些时间正确地完成它.在进行此操作之前,最好更好地了解内存所有权模型.

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协程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆