Swift inout關鍵字

1、inout定義

將值類型的對象用引用的方式傳遞?;蛘哒f地址傳遞。
類似于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
關羽 武圣
關羽 武圣
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容