Swift中的函数常见写法
这里不涉及函数作为参数和返回值的情况。
进军iOS开发了哈。
计划六一前,搞一个套H5的App出来,
靠谱么?
其实,看过了java,php,javascript,python,go之后,
再在看swift,感觉很亲切啊,
都是老熟人。
func greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting } print(greet(person: "Anna")) print(greet(person: "Brian")) func sayHelloWorld() -> String { return "hello, world" } print(sayHelloWorld()) func greet2(person: String) { print("Hello, \(person)!") } greet2(person: "Dave") func printAndCount(string: String) -> Int { print(string) return string.characters.count } func printWithoutCounting(string: String) { let _ = printAndCount(string: string) } printAndCount(string: "Hello, world") printWithoutCounting(string: "Hello, world") func minMax(array: [Int]) -> (min: Int, max: Int)? { if array.isEmpty {return nil} var currentMin = array[0] var currentMax = array[0] for value in array[1..<array.count] { if value < currentMin { currentMin = value } else if value > currentMax { currentMax = value } } return (currentMin, currentMax) } if let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) { print("min is \(bounds.min) and max is \(bounds.max)") } func gt(person: String, from hometown: String) -> String { return "Hello \(person)! glad you could visit from \(hometown)." } print(gt(person: "Bill", from: "Cupertino")) func someFunc(_ firstP: Int, secondP: Int) { print(firstP + secondP) } someFunc(1, secondP: 5) func someFunc2(pWithoutD: Int, pWithD: Int = 12) { print(pWithoutD + pWithD) } someFunc2(pWithoutD: 5) func arithmeticMean(_ numbers: Double...) -> Double { var total: Double = 0 for number in numbers { total += number } return total / Double(numbers.count) } print(arithmeticMean(1, 2, 3, 4, 5)) print(arithmeticMean(3, 5.34, 12.38, 67, 21.97)) func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a; a = b b = temporaryA } var someInt = 3 var anotherInt = 107 swapTwoInts(&someInt, &anotherInt) print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Gson把Java POJO和Json数据字符串之间相互转换
Gson把Java POJO和Json数据字符串之间相互转换 例如: POJO pojo = new POJO(); pojo.name = "zhangphil"; pojo.id = 1; pojo.strings = new ArrayList<>(); pojo.setBlog("zhangphil @ csdn"); for (int i = 0; i < 10; i++) { pojo.strings.add(String.valueOf(i)); } Gson gson = new Gson(); String s = gson.toJson(pojo); Log.d("POJO转换为Json字符串", s); POJO pojo2 = gson.fromJson(s, POJO.class); Log.d("Json字符串转换为POJO", pojo2.name + " , " + pojo2.id + " , " + pojo2.getBlog() + " , " + pojo2.strings.size()); POJO: private clas...
- 下一篇
C# /VB.NET 创建PDF项目符号列表和多级编号列表
使用项目符号和编号,可以让文档的层次结构更清晰、更有条理,也更容易突出重点。在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息。在之前的文章中,介绍了如何在Word中来创建项目标号和编号列表,在本篇文章中,将介绍创建PDF项目符号列表和多级编号列表的方法。 借助工具:Spire.PDF.dll PS:dll可以直接在安装路径下的Bin文件夹中获取。 1.创建PDF符号列表 C# using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Lists; namespace CreateList_PDF { class Program { static void Main(string[] args) { //创建一个PDF文档 PdfDocument doc = new PdfDocument(); //添加一页A4大小的页面 PdfPageBase page = doc.Pages.Add(PdfPageSize.A4); float y = 1...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装