Swift知识集iOS学习iOS Developer

[Swift]结构体指针操作

2016-10-18  本文已影响374人  冰琳92

C语言的指针操作

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

int a = 1;

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

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

a value is 2

c语言操作结构体指针操作
typedef struct User{
    int ID;
    int age;
} User;
User user;
user.ID = 1;
user.age = 3;
//申明一个结构体指针变量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>
Type * UnsafeMutablePointer<Type>

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

C 语法 Swift 语法
Type * const * UnsafePointer<Type>
Type * __strong * UnsafeMutablePointer<Type>
Type ** AutoreleasingUnsafeMutablePointer<Type>

在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>
//打印user指针的值
print(userPointer.pointee)
//User(ID: 1, age: 3)

//获取user ID的指针
let userIDPointer = unsafeBitCast(userPointer, to: UnsafeMutablePointer<Int>.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 中的指针使用

上一篇下一篇

猜你喜欢

热点阅读