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
关羽 武圣
关羽 武圣
推荐阅读
- Swift中willSet和didSet的简述
- 越努力越幸福
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- LeetCode算法题-11.|LeetCode算法题-11. 盛最多水的容器(Swift)
- iOS-Swift-map|iOS-Swift-map filter reduce、函数式编程
- Swift|Swift ----viewController 中addChildViewController
- SwiftUI|SwiftUI iOS 瀑布流组件之仿CollectionView不规则图文混合(教程含源码)
- Swift高级应用|Swift高级应用 -01
- 精准!找到想要的关键字
- Swift5.0|Swift5.0 UITexview的基本使用