Swift: gettimeofday and Unsafe Pointers

流过昼夜 提交于 2019-12-02 05:51:43

问题


The code in Swift

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

The code in Objective C

...
struct timeval time;
gettimeofday(&time, 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.

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!


回答1:


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)

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



来源:https://stackoverflow.com/questions/24655213/swift-gettimeofday-and-unsafe-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!