[ Swift # 函数 ]

 [ Swift # 函数 ]Swift学习

  Swift是Apple在WWDC2014所发布的一门编程语言,用来撰写OS X和iOS应用程序[1]。在设计Swift时.就有意和Objective-C共存,Objective-C是Apple操作系统在导入Swift前使用的编程语言

  Swift是供iOS和OS X应用编程的新编程语言,基于C和Objective-C,而却没有C的一些兼容约束。Swift采用了安全的编程模式和添加现代的功能来使得编程更加简单、灵活和有趣。界面则基于广受人民群众爱戴的Cocoa和Cocoa Touch框架,展示了软件开发的新方向。

 

================ 

 

// # 函数 func

println("[===内部和外部参数===]");

// MARK: 内部和外部参数(加#)

func say(MyName name:String, MyAge age:Int) {

   println("hello name=\"\(name)\" "+"\n\n" + " age=\"\(age)\"")

}



//say("mb", 22)

say(MyName: "mb", MyAge: 22)



// 带#

func say(#name:String, #age:Int) {

   println("hello \(name) \(age)")

}



say(name: "mb", age: 27)

 

// MARK: 交换 inout (取地址,交换)

func swap(inout a: Int, inout b:Int)

{

   var temp = a;

   a = b;

   b = temp;

}



var x = 10;

var y = 20;

println("交换前 a=\(x) b=\(y)");



swap(&x, &y);

println("交换后 a=\(x) b=\(y)");



// 交换

func swap1(var a:Int, var b:Int)

{

   var temp = a;

   a = b;

   b = temp;

}



var x1 = 10;

var y2 = 20;

println("Before exchanging."+"\n\n"+" a=\(x) b=\(y)");



swap1(x1, y2);



println("After exchanging."+"\n\n"+" a=\(x) b=\(y)");

 

// 函数 变参

func add(arr:Array<Int>) -> Int{

   var sum : Int = 0;

   

   for i in arr {

       sum += i;

   }

   

   return sum;



}



println(add([1, 2, 3]));

 

func add(#a:Int, #arr:Int...) -> Int {

   var sum:Int = 0;

   

   for i in arr {

       sum += i;



   }



   return sum + a;

}





//println(add(7, [1, 2, 3]));

 ------

分享:

Swift苹果官方版:|--> Download

 

================

PS:

[ 每日一句 

" There's always more to learn, and there are always better ways to do what you've done before. " -- Donald Ervin Knuth

 

推荐网址

http://www.cocoachina.com/ 

================ 
 

|-> Copyright (c) 2015 Bing Ma.

|--> GitHub: https://github.com/SpongeBob-GitHub

================

你可能感兴趣的:(swift)