星空网 > 软件开发 > 操作系统

Swift的笔记和工具类

Create Class 创建类 (重载效果)

Swift的笔记和工具类images/loading.gif' data-original="http://common.cnblogs.com/images/copycode.gif" />
// Create Class 创建类class MyClass {  // Properties 成员变量    init() {    // Constructor 构造函数  }    // Method 成员方法  func doIt() {    println("doIt")  }    func doIt() -> Int {    return 0  }    func doIt(a:Int) -> Int {    return a  }    func doIt(a:Int, b:Int) -> Int {    return a + b  }    func doIt() -> String {    return ""  }    func doIt(a:String) -> String {    return a  }    func doIt(a:String, b:String) -> String {    return a + b  }}// Create / Using an Instance 创建 / 使用 一个实例var a = MyClass()a.doIt("Wang ", b: "Zhipeng")
Swift的笔记和工具类

 

Enums 枚举

Swift的笔记和工具类
// Enums 枚举enum ComcSoftType: Int {    case DevelopmentEngineer = 1    case TestEngineer = 2  }var myType = ComcSoftType.DevelopmentEngineer
Swift的笔记和工具类

 

Declaring Variables 变量的声明 (可选变量)

Swift的笔记和工具类
// Declaring Variables 变量的声明var mutableDouble:Double = 1.0mutableDouble = 2.0let constantDouble:Double = 1.0//constantDouble = 2.0 Error 错误var autoDouble = 1.0// Optional Value 可选变量 (新机制)var optionDouble:Double? //此刻 optionDouble 根本没有分配内存,对其取地址: &optionDouble 为NULLoptionDouble = 1.0 //这时候开始 optionDouble 才会开始分配内存if let defineDouble = optionDouble {  println("已经分配内存")}else {  println("没有分配内存")}
Swift的笔记和工具类

 

Control Flow 控制流

Swift的笔记和工具类
// Control Flow 控制流var condition = trueif condition {  println("正确")}else {  println("错误")}var val = "Four"switch val {  case "One":    "One"  case "Two", "Three":    "Two, Three"  default:    "default"}// omits upper value, use ... to include 省略了上限值,使用 ... 包括for i in 0..3 {  println("i = \(i)")}for var j = 0; j < 3; ++j {  println("j = \(j)")}// Whilevar n = 2while n < 100 {  n = n * 2}println(n)var m = 2do {  m = m * 2} while m < 100println(m)
Swift的笔记和工具类

 

String Quick Examples 字符串的例子

Swift的笔记和工具类
// String Quick Examples 字符串的例子var firstName = "Zhipeng"var lastName = "Wang"var helloString = "Hello, \(lastName) \(firstName)"var tipString = "2499"var tipInt = tipString.toInt()extension Double {  init (string:String) {    self = Double(string.bridgeToObjectiveC().doubleValue)  }}tipString = "24.99"var tipDouble = Double(string:tipString)
Swift的笔记和工具类

 

Array Quick Examples 数组的例子

Swift的笔记和工具类
// Array Quick Examples 数组的例子var person1 = "One"var person2 = "Two"var array:String[] = [person1, person2]array += "Three"for person in array {  println("person: \(person)")}var personTwo = array[1]println("personTwo: \(personTwo)")
Swift的笔记和工具类

 

Dictionary Quick Examples 字典的例子 (泛型效果)

Swift的笔记和工具类
// Dictionary Quick Examples 字典的例子var dic:Dictionary<String, String> = ["One": "1",                   "Two": "2",                   "Three": "3"]dic["Three"] = "4" // Update Threedic["One"] = nil // Delete Onefor(key, value) in dic {  println("key: \(key), value: \(value)")}
Swift的笔记和工具类

 

Function 函数

Swift的笔记和工具类
// Function 函数func getPirces() -> (Double, Double, Double) {  return (1.1, 1.2, 1.3) // tuple 元组}// 取出元组var (one, two, three) = getPirces()println("One = \(one), Two = \(two), Three = \(three)")func getSum(numbers:Int...) -> Int {  var sum = 0  for number in numbers {    sum += number  }  return sum}getSum()getSum(1, 3, 5, 7, 9)
Swift的笔记和工具类

 

函数嵌套

Swift的笔记和工具类
// 函数嵌套func nestedFunction() -> Int {  var x = 0  //  func add() {    x += 1  }  //  add()  return x}
Swift的笔记和工具类

 

函数传递

Swift的笔记和工具类
// 函数传递func makeIncrementer() -> (Int -> Int) {  func addOne(number:Int) -> Int {    return number + 1  }  return addOne}var increment = makeIncrementer()increment(1)func makeIncrementer2() -> ((String, String) -> String) {  func and(a:String, b:String) -> String {    return a + b  }  return and}var increment2 = makeIncrementer2()increment2("Wang ", "zhieng")func lessThanOne(number:Int) -> Bool {  return number < 1}func makeIncrementer3(list:Int[], condition:Int -> Bool) -> Bool {  for number in list {    if condition(number) {      return true    }  }  return false}makeIncrementer3([1,2,3,4,5], lessThanOne)
Swift的笔记和工具类

 

 Extends 继承 (多态效果)

Swift的笔记和工具类
// Extends 继承 (多态效果)println("======================= Extends 继承")class People {  // Eat 吃  func eat() {    println("吃")  }  // Sleep 睡  func sleep() {    println("睡")  }  // Work 工作  func work() {    println("工作")  }  // Said 说  func said() {    println("我是人")  }}class Chinese:People {  // Said 说  override func said() {    println("我是中国人")  }}class Americans:People {  // Said 说  override func said() {    println("我是美国人")  }}class Alien {  // Eat 吃  func unknown() {    println("外星人")  }}// 不支持多继承//class futureGenerations:Chinese, Americans { // Multiple inheritance from classes 'Chinese' and 'Americans'//  // Said 说//  override func said() {//    println("我是中国人和美国人的后代")//  }//}// 多态 必要条件:1.继承 2.重写 3.父类引用指向子类对象var people = People()people.said()people = Chinese()people.said()people = Americans()people.said()//people = Alien() Error 'Alien' is not convertible to 'People'//people.said()var chinese = Chinese()chinese.said()var americans = Americans()americans.said()
Swift的笔记和工具类

 




原标题:Swift的笔记和工具类

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外收款:https://www.goluckyvip.com/tag/33405.html
海外数字营销:https://www.goluckyvip.com/tag/33406.html
海外数字营销 宇周:https://www.goluckyvip.com/tag/33407.html
海外私域:https://www.goluckyvip.com/tag/33408.html
海外送货:https://www.goluckyvip.com/tag/33409.html
FBA:https://www.goluckyvip.com/tag/3341.html
美众议院对TikTok“动手”,下一步该怎么在TikTok上营销?:https://www.kjdsnews.com/a/1836587.html
速卖通在韩国争议不断,投诉量激增两倍:https://www.kjdsnews.com/a/1836588.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流