美文网首页
Swift inout关键字

Swift inout关键字

作者: 小王在努力 | 来源:发表于2020-12-01 10:24 被阅读0次

1、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汇编分析inout关键字

    因为inout关键字比较简单,因此该文章篇幅相对比较短小。我们直到在swift中inout通常用来在函数内修改外部...

  • swift 关键字inout

    关键字inout:当我们需要通过一个函数来改变函数外面变量的值(以引用方式传递)。通俗的讲:就是使用inout关键...

  • Swift inout关键字

    1、inout定义 将值类型的对象用引用的方式传递。或者说地址传递。类似于ObjectC中的二级指针的意思 2、O...

  • Swift inout关键字

    作者想要达到的实现:在Swift组件化搭建中,让属性传给闭包作为参数供外部Model构建函数使用,使用时在外部修改...

  • 2017-12-26

    swift泛型的使用 ''' func exchange( a: inout T, b : inout T){//...

  • Swift中一些常见的关键字二(if let,guard let

    在上一篇文章Swift中一些常见的关键字一(inout,defer,throw等)简单的介绍了几个关键字。本文续写...

  • swift之inout关键字

    在swift中,我们常常对数据进行一些处理。因为swift的计算属性,所以如果不是大量重复性处理,基本可以在set...

  • Swift之inout关键字

    在Swift中,常用的字符串、数组和字典,由OC中的NSString、NSArray和NSDictionary转变...

  • swift之inout

    swift中需要对参数只进行修改,需要用到inout 关键字,调用函数时加& 喜欢可以加Q群号:874826112...

  • inout 参数

    在函数的参数中,可以传inout类型参数,如下: 其中inout关键字,当引用相应有inout参数的函数时,参数前...

网友评论

      本文标题:Swift inout关键字

      本文链接:https://www.haomeiwen.com/subject/omgjwktx.html