Swift seems nice, but cannot write simple function such as converting array to UnsafePointer

I'm writing an iOS accelerated framework application with Swift and it's frequently necessary to convert Swift array to unsafe pointer. However the conversion function I wrote cannot be compiled. Does anybody know what's wrong with this function?
func aryToUnsafePointer<T>(ary:T[]) -> UnsafePointer<T> {
    return UnsafePointer<T>((&ary as CMutablePointer<T>).value)
The error is
'T[]' is not a subtype of '@lvalue $T1'

problem solved

thanks to Justin

It is not allowed to take "&" of argument directly, so changing function like this will work.

func aryToUnsafePointer<T>(ary:T[]) -> UnsafePointer<T> {
    var a = ary
    return UnsafePointer<T>((&a as CMutablePointer<T>).value)

Now I understand the reason, but it seems a bit odd to me, though.


Justin said…
I think this might be your solution:
Maker wannabe said…
> Justin

Thanks for the link! Copying argument to local variable solved the problem.

Popular posts from this blog

Subclassing and Signal connect on a same widget crashes PySide application on exit.

Calling OpenCV functions via Cython from Python 3.X.

Showing CPU/Memory usage on tmux status bar(tmuxのステータスバーにCPUとMemoryの使用状況を表示する)