Swift结构体指针操作

C语言的指针操作 在c语言中申明一个变量并通过指针修改该变量的值 int a = 1; int *p = &a; *p = 2; printf("a value is

C语言的指针操作

在c语言中申明一个变量并通过指针修改该变量的值

int a = 1;

int *p = &a;

*p = 2;

printf("a value is %d////n",a);

a value is 2

c语言操作结构体指针操作

  • 申明一个叫User的结构体

typedef struct User{

int ID;

int age;

} User;

  • 申明一个结构体User变量user 设置ID值为1 age值为18

User user;

user.ID = 1;

user.age = 3;

  • 通过指针对变量user的值进行修改

//申明一个结构体指针变量userpointer指向user的地址

User *userpointer = &user;

//修改ID的值为2

int *idPointer = (int *)userpointer;

*idPointer = 2;

//获得属性age所在指针

int *agePointer = ++ idPointer;

//修改user的age值为4

*agePointer = 4;

printf("user ID = %d, age = %d", user.ID, user.age);

user ID = 2, age = 4

Swift

Swift并不推荐对指针进行直接操作,  但仍提供了几种可以直接操作内存的指针类型,以下是c与Swift的语法对应表 ,使用 Type

做类型占用

对于返回值、变量和参数,使用一下对应表

C 语法Swift 语法
const Type *

UnsafePointer

Type *

UnsafeMutablePointer

对于类,使用一下语法对应

C 语法Swift 语法

Type const

UnsafePointer

Type __strong

UnsafeMutablePointer

Type **

AutoreleasingUnsafeMutablePointer

在Swift中无类型的指针,原始内存可以用UnsafeRawPointer 和UnsafeMutableRawPointer来表示

如果像不完整结构体的这样的c指针的值的类型无法用Swift来表示,则用OpaquePointer来表示

更多指针相关知识详见 文档

接下来,进行Swift版结构体指针操作

申明User结构体

struct User {

var ID: Int

var age: Int

}

申明变量,并获取变量地址, 创建一个user变量 ID初始值问为1 age初始值为3

通过指针设置ID值为2 设置age值为3

var user = User(ID: 1, age: 3)

let userPointer = withUnsafePointer(to: &user, {$0})//UnsafePointer

//打印user指针的值

print(userPointer.pointee)

//User(ID: 1, age: 3)

//获取user ID的指针

let userIDPointer = unsafeBitCast(userPointer, to: UnsafeMutablePointer

.self)

//设置ID的值为2

userIDPointer.pointee = 2

print(userPointer.pointee)

//User(ID: 2, age: 3)

//获取user age的指针

let agePointer = userIDPointer.advanced(by: 1)

agePointer.pointee = 4

print(userPointer.pointee)

//User(ID: 2, age: 4)

虽然c和Swift的语法差距有点大,但是原理是相同的

接下来我们看一个更复杂一点的例子

public struct Person { 

var age: Int

var firstName: String

var lastName: String

var phoneNumber: PhoneNumber

}

public struct PhoneNumber {

var number: String

var type: String

}

创建person变量 并通过指针操作 获取对应的属性值

let phone = PhoneNumber(number: "186xxxxxxxx", type: "work")

var person = Person(age: 24, firstName: "Bing", lastName: "lin", phoneNumber: phone)

let rawPointer = withUnsafePointer(to: &person, { UnsafeRawPointer($0)})

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")

到这里,我们已经学会了 通过指针获取值,通过指针设置对应的值

接下来 就可以运用这些知识发挥自己的创造力了

参考资料

结构体的内存空间分配原理

Swift 中的指针使用

文章转自 冰琳92的简书

未登录用户
全部评论0
到底啦