Swift|Swift inout关键字

1、inout定义 【Swift|Swift inout关键字】将值类型的对象用引用的方式传递。或者说地址传递。
类似于ObjectC中的二级指针的意思
2、ObjectC中二级指针的使用

- (void)test{ NSString *name = @"name"; NSString *otherName = @"otherName"; [self getName:&name otherName:&otherName]; NSLog(@"%@ %@",name,otherName); } - (void)getName:(NSString **)name otherName:(NSString **)otherName{ NSLog(@"%@ %@",*name,*otherName); *name = @"关羽"; *otherName = @"武圣"; NSLog(@"%@ %@",*name,*otherName); }

  • 打印输出
2020-11-30 16:33:39.226129+0800 Demo[43449:375299] name otherName 2020-11-30 16:33:40.778225+0800 Demo[43449:375299] 关羽 武圣 2020-11-30 16:33:42.103776+0800 Demo[43449:375299] 关羽 武圣

3、Swift中inout的使用
func test(){ var name = "name" var otherName = "otherName" getName(name: &name, otherName: &otherName) print(name,otherName) } func getName(name : inout String,otherName : inout String){ print(name,otherName) name = "关羽" otherName = "武圣" print(name,otherName) }

  • 打印输出
name otherName 关羽 武圣 关羽 武圣

    推荐阅读