swiftiOS 开发 iOS Developer

swift 笔记

2016-06-02  本文已影响65人  LovelyYilia

**声明:swift 版本现在还不稳定,特性随时会变,有些结论不正确,我会尽量纠正。各位同行们,如果发现错误也请帮忙指正一下哈。谢谢了☺
**

基础

import UIKit
var one = 1
func changeOne() {
    let two = 2
    func sayTwo() {
        print(two)
    }
    class Klass {}
    struct Struct {}
    enum Enum {}
    one = two
}
class Manny {
    let name = "manny"
    func sayName() {
        print(name)
    }
    class Klass {}
    struct Struct {}
    enum Enum {}
}
struct Moe {
    let name = "moe"
    func sayName() {
        print(name)
    }
    class Klass {}
    struct Struct {}
    enum Enum {}
}
enum Jack {
    var name : String {
        return "jack"
    }
    func sayName() {
        print(name)
    }
    class Klass {}
    struct Struct {}
    enum Enum {}
}
//返回值为空
func say1(s:String) -> Void { print(s) }
func say2(s:String) -> () { print(s) }
func say3(s:String) { print(s) }

// 参数为空
func greet1(unused:Void) -> String { return "howdy" }
func greet2() -> String { return "howdy" }

// 参数和返回值为空
func greeet1(unused:Void) -> Void { print("howdy") }
func greeet2() -> () { print("howdy") }
func greeet3() { print("howdy") }
func say() -> String {
    return "one"
}

func say() -> Int {
    return 1
}
let stringValue: Int = say()
class Dog {
    func say(s:String, times:Int) {
        for _ in 1...times {
            print(s)
        }
    }
    func say(s:String) {
        print("OK")
    }
}
let d = Dog()
d.say("woof") // same as saying d.say("woof", times:1)
//d.say("woof", times:3)
func doThing (a a:Int = 0, b:Int = 3) {}
func countDownFrom(ix:Int) {
    print(ix)
    if ix > 0 { // stopper
        countDownFrom(ix-1) // recurse!
    }
}

关键字

在swift官方教程中大致意思说,虽然结构体和枚举可以定义自己的方法,但是默认情况下,实例方法中是不可以修改值类型的属性;

为了能够在实例方法中修改属性值,可以在方法定义前添加关键字 mutating;

这里in-out作为函数声明时,引用传值的关键字。相当于C#和Java中得ref和out。但是在函数调用的时候要写一个“&”符号在参数前面。

func swapTwoInts(inout a: Int, inout b: Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, b: &anotherInt)
print("someInt is now /(someInt), and anotherInt is now /(anotherInt)")

在swift中对于enum和struct来说支持用static关键字来标示静态“变量”;但是对于class成员来说,只能以class var的方式返回一个“只读”值。static 定义“常量”

class SomeClass {
    class var computedTypeProperty: Int {
        return 100
    }
}
class Test {
    static let ConstantValue: String = "TestString"
}
struct Duden {
    let offset:Int
    var textCount:Int
    subscript(index:Int) -> Int{
        get{
            return index - offset
        }
        set(newvalue){
            textCount = newvalue * 3
        }
    }
}
var duden = Duden(offset:2,textCount:0)
duden[9]  //7
duden[9] = 8  //duden.textCount  24

convenience用来进行方便的初始化,说白了就相当于构造函数重载,对于class来讲,默认或指定的初始化方法作为所谓的Designated初始化,若重载的初始化需要调用Designated初始化则将它作为convenience初始化,在方法前要加上convenience关键字。

if #available(iOS 9.0, *) {
//    let arr = self.view.layoutGuides
} else {
    // Fallback on earlier versions
}
#if DEBUG
print("hello")
#endif
#if !TARGET_IPHONE_SIMULATOR
//[KMCGeigerCounter sharedGeigerCounter].enabled = YES;
#endif

注释方法

/// This method does nothing
func foo() {
    // content
}
/**
 This method has parameters and a return type.
 
 - Parameter input: This is an input parameter.
 - Returns: True if it worked; false otherwise.
 */
func foo3(input: String) -> Bool {
    // content
    print("Hello Yilia")
    return true
}

分段

// MARK: - Section Name
// MARK: Sub-section Name
/*
Section: Singleton                                               -单例
Section: Declared Types (Enums, Structs, etc.), Type Aliases     -类型定义
Section: Constants                                               -常量
Section: Class Properties                                        -类属性
Section: Instance Properties                                     -实例属性
Sub-section: Stored                                                 实例存储属性
Sub-section: Computed                                               实例计算属性
Section: Init/Deinit                                             -初始化、析够
Section: Class Methods                                           -类方法
Sub-section: Public                                                公共类方法
Sub-section: Private                                               私有类方法
Section: Instance Methods                                        -实例
Sub-section: Public                                                公共实例方法
Sub-section: Private                                               私有实例方法
Section: Protocols                                               -委托
Sub-section: <Protocol Name>                                       委托名
*/
上一篇下一篇

猜你喜欢

热点阅读