Swift:gettimeofday和不安全指针 [英] Swift: gettimeofday and Unsafe Pointers

查看:154
本文介绍了Swift:gettimeofday和不安全指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift中的代码

...
var time:timeval?
gettimeofday(UnsafePointer<timeval>, UnsafePointer<()>) // this is the method expansion before filling in any data
...

Objective C中的代码

The code in Objective C

...
struct timeval time;
gettimeofday(&time, NULL);
...

我一直在尝试找到有关UnsafePointer以及传递NULL的替代方法的更多信息,但是我可能在树错了树.

I have been trying to find more information on UnsafePointer and alternatives to passing NULL, but I may be barking up the wrong tree.

如果有人知道如何在Swift中运行等效代码,那将是很好的.如果对发生的事情有很好的解释,那就更好了!

If anyone knows how to get the equivilant code working in Swift, that would be great. If there is a good explanation of what's going on with it that would be even better!

推荐答案

我知道一种解决方法,如下所示:

I know one way to do it and this is as follows:

var time:timeval = timeval(tv_sec: 0, tv_usec: 0)
gettimeofday(&time, nil)

我必须用某种东西来初始化time,所以实际上在指向的地址&time中有一个结构.

I had to initialize time with something so there actually was a struct at the address &time pointed to.

这篇关于Swift:gettimeofday和不安全指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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