swift之inout

在swift中,我们常常对数据进行一些处理。因为swift的计算属性,所以如果不是大量重复性处理,基本可以在set及didSet中改变原数据的一些状态。但需要用到同样的算法处理大量数据的时候,仍然需要写方法来解决。在如C一类的传统语言中,指针变量能轻易帮我们处理并直接修改掉原数据,而apple本身期望siwft中尽量减少指针的出现,因此,swift常规方法中经常用到的是值传递。值传递最明显的后果便是无法对原数据进行直接修改。如果我们需要处理后的数据结果,那么就需要重新定义一个变量来接收值。在原数据被废弃的情况下,这样既增多了代码量,也产生了空间大量浪费。因此 siwft提供了关键字修饰inout来申明数据地址传递,也称之为引用传递。在siwft3.0中 inout的位置发生了改变,处于标签位置,不过其作用相同。具体作用如下图代码:

以上代码是一个简单的冒泡排序。输出内容分别是排序结果和经过第一次、第二次调用方法后的原始数据。认真看一下代码的不同,就能很轻易的了解inout的作用了。在没有经过inout修饰参数中,传入数组,对数组进行排序。但无法影响原数组。当我们需要对处理后的数据结果进行操作的时候,就不得不定义新的变量来存储处理后的数组结果,而在第二个排序方法中,传入的是需要排序的数组地址,导致数组本身被影响,其原值也被修改。如果需要对结果进行操作,可以直接使用原数据。因此,无需返回数据,其正确代码如下:

在siwft中,除了class是默认引用传递外,其他数据类型如float,struct等等都属于值传递。如果我们在对其进行处理的时候希望能够在函数中直接对其原值进行修改直接修改,那么最好的方法就是直接使用inout来修饰传入参数,值得注意的是inout 无法修饰带有默认值的参数且经过inout修饰之后,无法再被let和var修饰

转载于:https://www.cnblogs.com/lingluo/p/6020784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write and run Swift language programs in the Cloud Written by the team of developers that has helped bring the Swift language to Cloud computing, this is the definitive guide to writing and running Swift language programs for cloud environment. In Swift in the Cloud, you'll find full coverage of all aspects of creating and running Swift language applications in Cloud computing environments, complete with examples of real code that you can start running and experimenting with today. Since Apple introduced the Swift language in 2014, it has become one of the most rapidly adopted computer programming languages in history—and now you too can start benefitting from using the same programming language for all components of a scalable, robust business software solution. Create server applications using Swift and run them on pay-as-you-go cloud infrastructure Quickly write and test Swift code snippets in your own cloud sandbox Use Docker containers to deploy Swift applications into multiple cloud environments without having to change code Grasp the elements and structure of the Swift.org open technology project Find out how to avoid the complexities of runtime configuration by using Cloud Foundry buildpacks for Swift Build high performing web applications and REST APIs with an open source Swift based web server framework Scale up your cloud services by running Swift modules in an asynchronous, open source, 'serverless' cloud environment Whether you are already using Swift to build mobile applications or a seasoned web developer, Swift in the Cloud will help you leverage server-side Swift to power your next generation of applications. Table of Contents Chapter 1 Swift.Org, The Open Source Project Chapter 2 A Swift Sandbox In The Cloud Chapter 3 A Basic Introduction To Swift Chapter 4 The Ibm Bluemix Buildpack For Swift Chapter 5 Using Containers On Bluemix To Run Swift Code Chapter 6 Swift Package Management Chapter 7 Swift And Kitura For Web Applications Chapter 8 Serverless Programming With Swift Chapter 9 Over The Horizon: Where Do We Go From Here?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值