Use of pointers in Swift (GO)

Source: Internet
Author: User

Pointers in SWIFT are mapped to generics

Unsafepointer<t>

Unsafemutablepointer<t>

A unsafebufferpointer<t> that represents a set of continuous data pointers

An opaque pointer copaquepointer that represents a non-complete structure, and so on

Unsafepointer<t> is evaluated by the memory property, and if the pointer is a variable unsafemutablepointer<t> type, we can also assign it by memory.

Func Incrementor (ptr:unsafemutablepointer<int>) {     ptr.memory + = 1}  var a = ten Incrementor (&a) A  

& in Swift can also take an address, but cannot get a pointer instance directly

var a = 10//let ptr:unsafemutablepointer<int> = &a//' inout Int ' is isn't convertible to ' unsafemutablepointer< ;int> '//let ptr2 = &a                          //Error func Incrementor1 (inout num:int) {    num + = 1}var B = 10incrementor1 (&b) b
   
    //11
   
[All-in-one] + 1  //No error, playground display an address value ([1] + 1) [ -100]  //Do not error ([1] ([])     Error//let ptr:unsafemutablebufferpointer<int> = array  //Error

When using the inout operator, variables declared with Var and constants using let declarations are converted to Unsafepointer and Unsafemutablepoinger respectively

The address of an existing object cannot be taken directly in Swift, and we can still create a new Unsafemutablepointer object. Unlike the automatic memory management of other objects in Swift, the management of pointers requires that we manually request and release the memory.

Will request 1 Int size memory to the system and return a pointer to this memory var intPtr = Unsafemutablepointer<int>.alloc (1)//Initialize Intptr.initialize (VAR) INTPTR2 = Intptr.successor () intptr2.initialize (50)//Read value Intptr.memory   //10intptr2.memory   //20// Intptr.dealloc (1)//intptr.destroy (1)//intptr.destroy () intPtr2 = nil//intptr2.memory  //dashed var array = [1,2,3]let Arrayptr = unsafemutablebufferpointer<int> (start: &array, Count:array.count)//baseaddress is the first element of a pointer var Baseptr = arrayptr.baseaddress as unsafemutablepointer<int>baseptr.memory//1baseptr.memory = 10basePtr.memory/ /10//Next element var nextptr = baseptr.successor () nextptr.memory//2

Direct Action variable address withunsafepointer,withunsafepointers

var test = 10test = Withunsafemutablepointer (&test, {(ptr:unsafemutablepointer<int>), Int in    Ptr.mem Ory + = 1    return ptr.memory}) test//11

Unsafebitcast

Unsafebitcast is a very dangerous operation, which forces a pointer-pointing memory to force a bitwise conversion to the target type. Because this conversion is done outside of the type management of Swift, the compiler cannot ensure that the resulting type is really correct, and you must know exactly what you are doing. Like what:

Let arr = Nsarray (object: "Meow") Let str = unsafebitcast (arr, 0), cfstring.self) str//"Meow" let AR r2 = ["Meow2"]let str2 = Unsafebitcast (Cfarraygetvalueatindex (arr2, 0), cfstring.self)

Because Nsarray can store arbitrary nsobject objects, when we use Cfarraygetvalueatindex to derive values from it, the result will be a unsafepointer<void>. Since we understand that the String object is stored, you can cast it directly to cfstring.

About unsafebitcast a more common usage scenario is the conversion between different types of pointers. Because the size of the pointer itself is certain, there is no fatal problem with the type of pointer being converted. This is common when collaborating with some C APIs. For example, there are many C API-required inputs that are void * and correspond to unsafepointer<void> in Swift. We can convert any pointer to unsafepointer in the following way.

var count = 100var voidptr = Withunsafepointer (&count, {a:unsafepointer<int>), Unsafepointer<void> ;    in return Unsafebitcast (A, Unsafepointer<void>.self)})//Voidptr is unsafepointer<void>. Equivalent to void *voidptr.memory//void//in C converted back to Unsafepointer<int>var intPtr = Unsafebitcast (Voidptr, Unsafepointer<i nt>.self) Intptr.memory//100

Use of pointers in Swift (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.