Swift 中的函数(中)

学习来自《极客学院:Swift中的函数》

工具:Xcode6.4

直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会

 1 import Foundation
 2
 3 //函数
 4
 5 //1.多个返回值
 6 func area(width: Double,height: Double) -> (Double,Double)
 7 {
 8     var b = width
 9     var a = width * height
10     return (a,b)
11 }
12 println(area(2,3))
13 //2、函数类型
14 func addTwoInts(a: Int, b: Int) -> Int{
15     return a + b
16 }
17 var function: (Int,Int) ->Int = addTwoInts
18 println(function(3,4))
19 //3.函数类型作为参数类型
20 func outfunc(function_1: (Int,Int) ->Int,m:Int,n:Int){
21     println("result:\(function_1(m, n))")
22 }
23 outfunc(addTwoInts ,12,12)
24 //4函数类型作为返回值类型
25 func square(a :Int)->Int{
26     return a*a
27 }
28 func cube(a:Int)->Int{
29     return a*a*a
30 }
31 func getMatchFunction(str:String)->(Int)->Int{
32     switch(str){
33     case "square":
34         return square
35     default:
36         return cube
37     }
38 }
39 var mathFunc = getMatchFunction("cube")
40 println(mathFunc(5))
41 //函数重载
42 func test(){
43     println("无参数的test函数")
44 }
45 func test(msg:String){
46     println("重载的test()函数\(msg)")
47 }
48 func test(msg:String) -> String{
49     println("重载的test()函数,外部参数为\(msg)")
50     return "test"
51 }
52 func test(#msg:String) {
53     println("重载的test()函数,外部参数为\(msg)")
54 }
55 test()
56 var result: Void = test(msg: "何杨")
57 var result2: String = test("哇哦")
58 /*局部参数名不能作为区分重载 运行会报错
59 func test(message:String){
60     println("")
61 }
62 */

运行的结果:

时间: 2024-08-29 00:41:43

Swift 中的函数(中)的相关文章

软件测试中LoadRunner函数中的几个陷阱

软件测试 中 LoadRunner 函数中的几个陷阱 1.atof 在 loadrunner 中如果直接用 float f; f=atof("123.00"); lr _output_message("%f",f); 输出的结果会是1244128.00,根本不是我们想要的. 因为float,double型在不同的平台下长度不一样,所以在loadrunner 软件测试中LoadRunner函数中的几个陷阱 1.atof 在loadrunner中如果直接用 float

苹果浏览器Safari对JS函数库中newDate()函数中的参数的解析中不支持形如“2020-01-01”形式

苹果浏览器safari对new Date('1937-01-01')不支持,用.replace(/-/g, "/")函数替换掉中划线即可 如果不做处理,会报错:invalid date 本解决方案参考:http://stackoverflow.com/questions/4310953/invalid-date-in-safari

Javascript中的函数中的this值

看下面这段代码会在控制台上输出什么内容? 1 <script> 2 var url="fang.com"; 3 var obj={ 4 url:"soufun.com", 5 func:function(){ 6 return this.url; 7 } 8 }; 9 10 console.log((obj.func)()); 11 console.log((1&&obj.func)()) 12 </script> 答案是 1

继承过程中对函数中this的认识

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <script> 9 var a = { 10 x:10, 11 cc:function(z){ 12 return this

Python中print函数中中逗号和加号的区别

先看看print中逗号和加号分别打印出来的效果.. 这里以Python3为例 1 print("hello" + "world") helloworld 1 print("hello", "world") hello world 这里发现加号的作用是连接字符串 而逗号相当于用空格连接字符串. 尝试一下不同数据类型的操作.. 1 print("hello" + 123) TypeError: must be

React中render函数中变量map中事件无法关联的解决办法

如下所示的代码,Input的checkbox可以正常显示3个,但是都无法和 handleChange关联上. 由于代码无法正常显示,我用图片 var Input = ReactBootstrap.Input; var TestCom = React.createClass({ getInitialState: function() { return {value: 'Hello!', value2: 'nihao2' }; }, handleChange: function(event) { v

泊松表面重建中主函数中部分代码分析-关于内存设置

1 //总体来看是和内存设置有关的 2 #if defined(WIN32) && defined(MAX_MEMORY_GB) 3 if( MAX_MEMORY_GB>0 ) 4 { 5 //SIZE_T是ULONG_PTR类型又是unsigned __int64类型取值范围为2到2的64次方,貌似和64为操作系统支持的理论内存值有关系 6 SIZE_T peakMemory = 1; 7 peakMemory <<= 30;//peakMemory等于peakMemo

AE中OnAfterDraw函数中绘制点线面的相关代码

需要提前得到一个IActiveview类型的变量activeView 1.点绘制代码 其中getoffDrawList装载的是IFeature类型的点要素 1 if (getoffDrawList != null && getoffDrawList.Count > 0 ) 2 { 3 IRgbColor getOnOffPtcolor = new RgbColorClass(); 4 getOnOffPtcolor.Red = 0; 5 getOnOffPtcolor.Green =

urllib模块中parse函数中的urlencode和quote_plus方法

本来只是向看一下quote_plus的作用,然后发现urlencode方法也是很方便的一个组合字符串的方法首先是介绍一下urlencode,他是将一些传入的元素使用&串联起来,效果如下: >>>params = { "appid": 1, "mch_id": 1, "body": 1, "out_trade_no": 1, "total_fee": 1, "spbill_

把数据写入txt中 open函数中 a与w的区别

a: 打开一个文件用于追加.如果该文件已存在,文件指针将会放在文件的结尾. 也就是说,新的内容将会被写入到已有内容之后.如果该文件不存在,创建新文件进行写入. w:  打开一个文件只用于写入.如果该文件已存在则打开文件,并从开头开始编辑, 即原有内容会被删除.如果该文件不存在,创建新文件. 简单说a类似于append,每次运行在原有基础上增加,而w是覆盖. open模式设为a运行两次 代码: 1 filename = 'test.txt' 2 file = open(filename, 'a')