Swift的笔记和参考

原文: Swift的笔记和参考

好久没来了,趁着新语言Swift发布,继续钻研中!

Create Class 创建类 (多态效果)

// 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")

 

Enums 枚举

// Enums 枚举
enum ComcSoftType: Int {
    
    case DevelopmentEngineer = 1
    
    case TestEngineer = 2
    
}

var myType = ComcSoftType.DevelopmentEngineer

 

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

// Declaring Variables 变量的声明
var mutableDouble:Double = 1.0
mutableDouble = 2.0

let constantDouble:Double = 1.0
//constantDouble = 2.0 Error 错误

var autoDouble = 1.0

// Optional Value 可选变量 (新机制)
var optionDouble:Double? //此刻 optionDouble 根本没有分配内存,对其取地址: &optionDouble 为NULL
optionDouble = 1.0 //这时候开始 optionDouble 才会开始分配内存

if let defineDouble = optionDouble {
    println("已经分配内存")
}
else {
    println("没有分配内存")
}

 

Control Flow 控制流

// Control Flow 控制流
var condition = true
if 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)")
}

 

String Quick Examples 字符串的例子

// 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)

 

Array Quick Examples 数组的例子

// 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)")

 

Dictionary Quick Examples 字典的例子

var dic:Dictionary<String, String> = ["One": "1",
                                     "Two": "2",
                                     "Three": "3"]
dic["Three"] = "4" // Update Three
dic["One"] = nil // Delete One
for(key, value) in dic {
    println("key: \(key), value: \(value)")
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值