Swift Structural body pointer operation

Source: Internet
Author: User

Declare a variable in the C language and modify the value of the variable by pointer

int a = 1;

int *p = &a;
*p = 2;

printf ("A value is%d\\\\n", a);
A value is 2

C language Operation structure body pointer operation

Declare a struct called user

typedef struct user{
int ID;
int age;
} User;
Declare a struct user variable user setting ID value is 1 Age value is 18

User user;
User.ID = 1;
User.age = 3;
Modify the value of the variable user by using a pointer

Declare a struct pointer variable userpointer to the address of user
User *userpointer = &user;

The value of the Modify ID is 2
int *idpointer = (int *) Userpointer;
*idpointer = 2;

Get attribute Age pointer
int *agepointer = + + Idpointer;
Modify user's age value to 4
*agepointer = 4;
printf ("User ID =%d, age =%d", user.id, User.age);
User ID = 2, age = 4

Swift

Swift does not recommend direct manipulation of pointers, but it still provides several types of pointers that can manipulate memory directly, the following is the syntax table for C and swift, using type to occupy

For return values, variables, and parameters, use the corresponding table

C syntax Swift syntax
Const TYPE * Unsafepointer
Type * Unsafemutablepointer
For classes, use the syntax corresponding

C syntax Swift syntax
Type Const Unsafepointer
Type __strong Unsafemutablepointer
Type * * Autoreleasingunsafemutablepointer
No type of pointer in swift, raw memory can be represented by Unsaferawpointer and Unsafemutablerawpointer

If the type of the value of such a C pointer as an incomplete struct cannot be represented by Swift, it is represented by Opaquepointer

More pointers related knowledge See documentation

Next, the swift version of the structure pointer operation

Declare the user structure body

struct User {
var id:int
var age:int
}
Declare the variable and get the variable address, create a user variable id initial value Ask 1 Age initial value of 3

Set the age value to 3 by setting the ID value to 2 with the pointer

var user = User (id:1, age:3)
Let Userpointer = Withunsafepointer (to: &user, {$})//unsafepointer


Print the value of the user pointer
Print (Userpointer.pointee)
User (Id:1, Age:3)

Get a pointer to the user ID
Let Useridpointer = Unsafebitcast (Userpointer, To:unsafemutablepointer

. Self)
The value of the set ID is 2
Useridpointer.pointee = 2
Print (Userpointer.pointee)
User (Id:2, Age:3)

Get pointer to user age
Let Agepointer = useridpointer.advanced (by:1)
Agepointer.pointee = 4
Print (Userpointer.pointee)
User (Id:2, Age:4)

Although the grammar gap between C and Swift is a bit large, the principle is the same

Now let's look at a more complicated example

public struct Person {
var age:int
var firstname:string
var lastname:string
var phonenumber:phonenumber
}
public struct PhoneNumber {
var number:string
var type:string
}
Create the person variable and get the corresponding property value through the pointer operation

Let phone = phonenumber (number: "186xxxxxxxx", type: "Work")
var person = person (age:24, firstName: "Bing", LastName: "Lin", Phonenumber:phone)
Let Rawpointer = Withunsafepointer (to: &person, {unsaferawpointer ($)})

Let-age = Rawpointer.load (frombyteoffset:0, as:Int.self)
Let FirstName = Rawpointer.load (Frombyteoffset:8, as:String.self)
Let LastName = Rawpointer.load (frombyteoffset:32, as:String.self)
Let Phoneinfo = Rawpointer.load (frombyteoffset:56, as:PhoneNumber.self)

Print (Age: \\\\ (age) FirstName: \\\\ (firstName) LastName: \\\\ (LastName), PhoneNumber: \\\\ (Phoneinfo))
Age:24 firstname:bing Lastname:lin, Phonenumber:phonenumber (number: "186xxxxxxxx", type: "Work")
Here we've learned to get the value through the pointer, to set the corresponding value through the pointer

And then you can use that knowledge to develop your creativity.

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.