码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【Swift 60秒】55 - Shorthand parameter names


    0x00 Lesson

    We just made a travel() function. It accepts one parameter, which is a closure that itself accepts one parameter and returns a string. That closure is then run between two calls to print().

    Here’s that in code:

    func travel(action: (String) -> String) {
    	print("I'm getting ready to go.")
    	let descripton = action("London")
    	print("description")
    	print("I arrived!")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    We can call travel() using something like this:

    travel { (place: String) -> String in 
    	return "I'm going to \(place) in my car"
    }
    
    • 1
    • 2
    • 3

    However. Swift knows the parameter to that closure must be a string, so we can remove it:

    travel { place -> String in 
    	return "I'm going to \(place) in my car"
    }
    
    • 1
    • 2
    • 3

    It also knows the closure must return a string, so we can remove that:

    travel { place in
    	return "I'm going to \(place) in my car"
    }
    
    • 1
    • 2
    • 3

    As the closure only has one line of code that must be the one that returns the value, so Swift lets us remove the return keyword too:

    travel { place in
    	"I'm going to \(place) in my car"
    }
    
    • 1
    • 2
    • 3

    Swift has a shorthand syntax that lets you go even shorter. Rather than writing place in we can let Swift provide automatic names for the closure’s parameters. These are named with a dollar sign, then a number counting from 0.

    travel {
    	"I'm toing to \($0) in my car"
    }
    
    • 1
    • 2
    • 3

    0x01 我的小作品

    欢迎体验我的作品之一:小五笔
    五笔学习好帮手
    App Store 搜索即可~


  • 相关阅读:
    Spring框架讲解笔记:spring框架学习的要点总结
    C#毕业设计——基于C#+asp.net+sqlserver作业提交系统设计与实现(毕业论文+程序源码)——作业提交系统
    Matlab深度学习应用教程
    【Git】一文带你入门Git分布式版本控制系统(必要配置、工作原理、创建/克隆项目)
    深度解析 MetaArena 游戏引擎,如何让 GameFi 应用更具生命力?
    基础组件-流量回放(全链路流量回放预研)
    【AAAI2023】Spatial-Spectral Transformer for Hyperspectral Image Denoising
    NL2SQL技术方案系列(1):NL2API、NL2SQL技术路径选择;LLM选型与Prompt工程技巧,揭秘项目落地优化之道
    vue-cli4升级到vue-cli5过程记录
    Python与mqtt的数据读取
  • 原文地址:https://blog.csdn.net/xjh093/article/details/128037218
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号