Constant Pointers
When a function is declared as taking a
UnsafePointer<Type>
argument, it can accept any of the following:
nil
, which is passed as a null pointer. AnUnsafePointer<Type>
,UnsafeMutablePointer<Type>
, orAutoreleasingUnsafeMutablePointer<Type>
value, which is converted toUnsafePointer<Type>
if necessary. AString
value, ifType
isInt8
orUInt8
. The string will automatically be converted to UTF8 in a buffer that lasts for the duration of the call. Aninout
expression whose left-hand side operand is of typeType
, which is passed as the address of the left-hand side identifier. A[Type]
value, which is passed as a pointer to the start of the array, and lifetime-extended for the duration of the call.If you have declared a function like this one:
func takesAPointer(x: UnsafePointer<Float>) {
// ...
}
You can call it in any of the following ways:
var x: Float = 0.0
var p: UnsafePointer<Float> = nil
takesAPointer(nil)
takesAPointer(p)
takesAPointer(&x)
takesAPointer([1.0, 2.0, 3.0])
On Nov 5, 2015, at 2:13 PM, Ken Ferry <kenf...@gmail.com> wrote:For this purpose you should be able to pass &x directly to the function.
--You received this message because you are subscribed to the Google Groups "Swift Language" group.To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.To post to this group, send email to swift-l...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/55E45AF0-1AA5-4AB4-ACC4-071A981F1607%40mooseyard.com.For more options, visit https://groups.google.com/d/optout.
--You received this message because you are subscribed to the Google Groups "Swift Language" group.To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/d34582f3-cc00-4121-a06d-232ae546f2d5%40me.com.
On Nov 5, 2015, at 2:34 PM, Kevin Ballard <ke...@sb.org> wrote:Because of that, constructing an UnsafePointer<X> like you're doing is fundamentally unsafe; you're holding onto a pointer value past the scope where the pointer is valid.
There exists functions called withUnsafePointer() and withUnsafeMutablePointer() (and variants of these) that give you a pointer to a value that's valid for a nested scope. That's the supported way of working with pointers