michaelcheng1991 / Golf-Training-Aid-

Using sensors to create a golfer-friendly teaching device
0 stars 0 forks source link

Mark's lecture on Swift #17

Open michaelcheng1991 opened 9 years ago

michaelcheng1991 commented 9 years ago

C++ -> Java C# Garbabe Collection (think about the refrigerator in office) (new / delete)

https://www.apple.com/tw/watchos-2-preview/

https://zh.wikipedia.org/wiki/C%2B%2B11

https://en.wikipedia.org/wiki/C%2B%2B14

http://clang.llvm.org/

Swift 2.0

https://developer.apple.com/swift/blog/?id=29

http://stackoverflow.com/

user/user

Help/⇒ NSURLConnection

https://evernote.com/intl/zh-tw/skitch/

http://www.cplusplus.com/reference/tuple/tuple/

https://developer.apple.com

provision ⇒ 1 account ⇒ 100 devices

https://developer.apple.com/testflight/update/

https://developer.apple.com/programs/mfi/

Snippet:

// this is a comment
/* this is multiple line
comment
*/

var greeting:String = "Hello" // this means greeing is a string type
var ranking = 5
ranking = 6
//ranking = '7'
//ranking = "7"

//var wrongGreeting:String = 5

greeting += greeting

ranking += ranking

// print something to console
println("print something")
str.append(Character("!"));
println(str)

// in same line, use ; to separate
var a:Int=1; var b=2; var c:Int = 3;
var d = a+b+c;

var radius = 3
var  pi = 3.14159
var area = Double(radius) * Double(radius) * pi
~~~~step4~~~~

let radius = 3
let  pi = 3.14159
let area = Double(radius) * Double(radius) * pi
let areaString = "This circle has radius:" + String(radius)

println(areaString)

println("This circle has radius: \(radius)")

var iphone5 = 5
let statement1 = "I have \(iphone5) iphone5s"
var iphone6 = 3
let statement2 = "I have \(iphone5+iphone6) iphones"
let weight = 8.95
let statement3 = "total weight=\(weight) kgs"

var products = ["iphone5","iphone5s","iphone6","iphone6plus"]; println(products[3]); println("Hello, World!")

var products = ["iphone5","iphone5s","iphone6","iphone6plus"];
println(products[3]);
products[3]="iphone6 plus"
//println(products[4]);
//products[4] = "iphone 6 plus s";
println("Hello, World!")

import Foundation

var sales = ["iphone5":50,"iphone6":60,"iPhone6 plus":30];

var specs = ["iphone5":"3000mA","iphone6":"4000mA", "iPhone6 plus":"4500mA"]; var iphone5Sales = sales["iphone5"]; var iphone6Spec = specs["iphone6"]; sales["iphone6"] = 100; let iphone6Sales = sales["iphone6"]; println("sales for iphone6 =(iphone6Sales)"); let htcOneM9 = sales["htcOneM9"]; println("sales for htc=(htcOneM9)");

println("Hello, World!")

println("sales for htc=(htcOneM9)"); specs=[:] println("empty specs")

println("Hello, World!")

let numberStream = [
    "Prime": [2,3,5,7,11,13,17],
    "Fibonacci": [1,1,2,3,5,8,13,21],
    "Square": [1,4,9,16,25,36,49]
]
for (kind, numbers) in numberStream{
    println("current kind=\(kind):");
    for number in numbers {
        println("result=\(number)");
    }
}

//var greetingString :String = "Hello" var greetingString :String? = "Hello"

var stringIsNull = false if (greetingString == nil) { stringIsNull = true; } println("result = (stringIsNull)");

let product = "Apple"

switch product {
    case "Apple":
        println("good product");
        break;
default:
    break;

}

let product = "Pineapple"

switch product { case "Apple": println("good product"); break; case let x where x.hasSuffix("pple"): println("any product"); break; default: break;

}

http://www.erlang.org/

func sayHello(name:String, day:String, times:Int)->String { var result = ""; for var i=0; i<times; i++ { result += "Hi, (name), it's (day)" }

return result;

}

sayHello("Mark", "Labor day", 2)

let names = ["Mark","James", "John","Ken"]; func backSorts(name1:String, name2:String)->Bool { return name1 < name2; } var reverseName = sorted(names,backSorts);

var reverseName2 = sorted(names, { (name1:String, name2:String)->Bool in return name1 > name2; });

var reverseName4 = sorted(names, {$0<$1}) reverseName4

Day1 code snippet

https://drive.google.com/folderview?id=0B1rMVw7ZJoGifkRtbFJocEdUWHN2WnM3c291b1Z0NlBVUWFqaFZCRVE4dnJTbXJUamxlZ3c&usp=sharing

michaelcheng1991 commented 9 years ago

https://www.apple.com/tw/watchos-2-preview/

https://zh.wikipedia.org/wiki/C%2B%2B11

https://en.wikipedia.org/wiki/C%2B%2B14

http://clang.llvm.org/

Swift 2.0

https://developer.apple.com/swift/blog/?id=29

http://stackoverflow.com/

user/user

Help/⇒ NSURLConnection

https://evernote.com/intl/zh-tw/skitch/

http://www.cplusplus.com/reference/tuple/tuple/

https://developer.apple.com

provision ⇒ 1 account ⇒ 100 devices

https://developer.apple.com/testflight/update/

https://developer.apple.com/programs/mfi/

Snippet:

// this is a comment
/* this is multiple line
comment
*/

var greeting:String = "Hello" // this means greeing is a string type
var ranking = 5
ranking = 6
//ranking = '7'
//ranking = "7"

//var wrongGreeting:String = 5

greeting += greeting

ranking += ranking

// print something to console
println("print something")
str.append(Character("!"));
println(str)

// in same line, use ; to separate
var a:Int=1; var b=2; var c:Int = 3;
var d = a+b+c;

var radius = 3
var  pi = 3.14159
var area = Double(radius) * Double(radius) * pi
~~~~step4~~~~

let radius = 3
let  pi = 3.14159
let area = Double(radius) * Double(radius) * pi
let areaString = "This circle has radius:" + String(radius)

println(areaString)

println("This circle has radius: \(radius)")

var iphone5 = 5
let statement1 = "I have \(iphone5) iphone5s"
var iphone6 = 3
let statement2 = "I have \(iphone5+iphone6) iphones"
let weight = 8.95
let statement3 = "total weight=\(weight) kgs"

var products = ["iphone5","iphone5s","iphone6","iphone6plus"]; println(products[3]); println("Hello, World!")

var products = ["iphone5","iphone5s","iphone6","iphone6plus"];
println(products[3]);
products[3]="iphone6 plus"
//println(products[4]);
//products[4] = "iphone 6 plus s";
println("Hello, World!")

import Foundation

var sales = ["iphone5":50,"iphone6":60,"iPhone6 plus":30];

var specs = ["iphone5":"3000mA","iphone6":"4000mA", "iPhone6 plus":"4500mA"]; var iphone5Sales = sales["iphone5"]; var iphone6Spec = specs["iphone6"]; sales["iphone6"] = 100; let iphone6Sales = sales["iphone6"]; println("sales for iphone6 =(iphone6Sales)"); let htcOneM9 = sales["htcOneM9"]; println("sales for htc=(htcOneM9)");

println("Hello, World!")

println("sales for htc=(htcOneM9)"); specs=[:] println("empty specs")

println("Hello, World!")

let numberStream = [
    "Prime": [2,3,5,7,11,13,17],
    "Fibonacci": [1,1,2,3,5,8,13,21],
    "Square": [1,4,9,16,25,36,49]
]
for (kind, numbers) in numberStream{
    println("current kind=\(kind):");
    for number in numbers {
        println("result=\(number)");
    }
}

//var greetingString :String = "Hello" var greetingString :String? = "Hello"

var stringIsNull = false if (greetingString == nil) { stringIsNull = true; } println("result = (stringIsNull)");

let product = "Apple"

switch product {
    case "Apple":
        println("good product");
        break;
default:
    break;

}

let product = "Pineapple"

switch product { case "Apple": println("good product"); break; case let x where x.hasSuffix("pple"): println("any product"); break; default: break;

}

http://www.erlang.org/

func sayHello(name:String, day:String, times:Int)->String { var result = ""; for var i=0; i<times; i++ { result += "Hi, (name), it's (day)" }

return result;

}

sayHello("Mark", "Labor day", 2)

let names = ["Mark","James", "John","Ken"]; func backSorts(name1:String, name2:String)->Bool { return name1 < name2; } var reverseName = sorted(names,backSorts);

var reverseName2 = sorted(names, { (name1:String, name2:String)->Bool in return name1 > name2; });

var reverseName4 = sorted(names, {$0<$1}) reverseName4

Day1 code snippet

https://drive.google.com/folderview?id=0B1rMVw7ZJoGifkRtbFJocEdUWHN2WnM3c291b1Z0NlBVUWFqaFZCRVE4dnJTbXJUamxlZ3c&usp=sharing

https://goo.gl/fle8Gk

let digitToChinese = [0:"零",1:"壹",2:"貳", 3:"參",4:"肆",5:"伍",6:"陸",7:"柒",8:"捌",9:"玖",10:"拾"]

let numbers = [147,258,369] let stringArrays = numbers.map({ (var number)->String in var output=""; while(number>0) { output = digitToChinese[number%10]!+output number/=10; } return output; })

stringArrays

let stringArrays2 = numbers.map(){ (var number)->String in var output=""; while(number>0) { output = digitToChinese[number%10]!+output number/=10; } return output; } stringArrays2

func contains(string:String, charToFind:Character)->Bool {
    for character in string {
        if charToFind == character {
            return true;
        }
    }
    return false;
}

let result1 = contains("Mark", "a")
let result2 = contains("Mark", "A")

func contains2(#string:String, #charToFind:Character)->Bool { for character in string { if charToFind == character { return true; } } return false; } let result3 = contains2(string: "James", charToFind: "J") let result4 = contains2(string: "James", charToFind:"j")


func concat2(string1:String,string2:String="DEFAULT",withJoin:String="  ")->String {
    return string1+withJoin+string2;
}
let myName3 = concat2("Mark", string2: "HO", withJoin: "####")
let myName4 = concat2("Mark", string2: "HO")
let myName5 = concat2("Mark")
// bad behavior
let wrongName6 = concat2("Mark",withJoin:"XXXX")

func swap(inout a:Int, inout b:Int) { let temp = a; a = b; b = temp; } var foo = 3 var bar = 6 swap(&foo, &bar) println("foo=(foo),bar=(bar)")

func addTwo(foo:Int,bar:Int)->Int {
    return foo+bar;
}
// define a function pointer (variable)
// type ==> parameter 2 Int, return 1 Int
var myFunction:(foo:Int,bar:Int)->Int = addTwo;

println("2+3 = \(myFunction(foo:2,bar:3))");

var myLazyFunction = addTwo;

println("3+4 = \(myLazyFunction(3,4))");

var myFunction2 = addTwo(100,200);
println("result=\(myFunction2)");

func summation(numbers:Int...)->Int { var sum = 0; for number in numbers { sum += number; } return sum; } summation(0) summation(1,2,3,4,5) summation(500,123,456)

[04-Aug-1:40]

func foo1() ->Int {
    var x = 10;
    func bar() {
        x += 20;
    }
    bar();
    bar();
    return x;
}
foo1();
foo1();

func bar1()->(Int->Int) {
    func boo(number:Int)->Int {
        return number*number*3;
    }
    return boo ;
}
var myVariable = bar1();
myVariable(3);
myVariable(4);

func increase(amount:Int)->()->Int { var runningTotal = 0 func incrementor()->Int { runningTotal += amount; return runningTotal; } // return a function, no parameter, return Int return incrementor; }

let result1 = increase(20); result1() result1()

let result2 = increase(5); result2() result2()

enum Status {
    case SUCCESS
    case FAIL
    case RETRY
    case ABORT
}

enum Status2 {
    case SUCCESS, FAIL, RETRY, ABORT
}

var connectionStatus = Status.ABORT

// for some reason, like retry
connectionStatus = .SUCCESS
switch(connectionStatus) {
case .ABORT:
    println("case abort");
    break;
    case .FAIL:
        break;
case .RETRY:
    break;
case .SUCCESS:
    break;
}
var connectionStatus2 = Status2.ABORT
switch(connectionStatus2) {
case .SUCCESS:
    break;
default:
    println("some default");
}

[class1]

var str = "Hello, playground" class Shape { var colorId = 0 func simpleDescription() -> String{ return "color id = (colorId)"; } }

var shape1 = Shape(); shape1.colorId = 5 shape1.simpleDescription()

// shape1 and shape2 will lead to same instance shape2.colorId = 10 shape1.simpleDescription()

class Shape {
    var colorId = 0
    func simpleDescription() -> String{
        return "color id = \(colorId)";
    }
}

var shape1 = Shape();
shape1.colorId = 5
shape1.simpleDescription()

var shape2 = shape1
shape2.simpleDescription()

// shape1 and shape2 will lead to same instance
shape2.colorId = 10
shape1.simpleDescription()

struct Figure {
    var colorId = 0
    func simpleDescription() -> String{
        return "color id = \(colorId)";
    }
}
var figure1 = Figure();
figure1.colorId = 5;
figure1.simpleDescription()

var figure2 = figure1
figure2.simpleDescription()

figure2.colorId = 10;
figure1.simpleDescription()
figure2.simpleDescription()

class Shape { var colorId = 0 func simpleDescription() -> String{ return "color id = (colorId)"; } } // shape1 address is immutable let shape1 = Shape(); shape1.colorId = 5 shape1.simpleDescription()

// shape2 address is mutable var shape2 = shape1 shape2.simpleDescription()

// shape1 and shape2 will lead to same instance shape2.colorId = 10 shape1.simpleDescription()

// shape2 assign to a new instance shape2 = Shape(); // if shape1 is constant (let) // shape1 can't re-allocate //shape1 = Shape();

var store1 = ["iphone6":10,"iphone6+":10,"iPad":10]
var store2 = store1

store1["iphone6"] = 8
store2["iPad"]=4

store1
store2

var sequence1 = [1,3,5,7,9] var sequence2 = sequence1 var sequence3 = sequence1 sequence1[0] = 300 sequence2[1] = 200 sequence3[2] = 100 sequence1 sequence2 sequence3

if sequence2 == sequence1 { println("same"); } else { println("No the same"); }

struct FixedLength {
    var initValue:Int
    let length:Int
}

var firstPosition = FixedLength(initValue: 3, length: 5)
firstPosition.initValue = 7
//firstPosition.length = 2

let secondPosition = FixedLength(initValue: 2, length: 4)
//secondPosition.length = 7
//secondPosition.initValue = 9

struct Point { var x=0,y=0 }

struct Size { var width=0, height=0 }

struct Rect { var origin = Point() var size = Size() var center:Point { get { let cX = origin.x+(size.width/2); let cY = origin.y+(size.height/2); return Point(x: cX, y: cY); } set(newCenter) { origin.x = newCenter.x-(size.width/2); origin.y = newCenter.y-(size.height/2);

    }
}

} var square1 = Rect(origin: Point(x:0,y:0), size: Size(width:4, height:4)); let newCenter = Point(x:5,y:5) square1.center = newCenter;

class Ticket {
    var _counter:Int = 0 {
        willSet(newValue) {
            println("will change to \(newValue)");
        }
        didSet {
            println("already set to \(oldValue)");

        }
    }
}

let myTicketMachine = Ticket();
myTicketMachine._counter = 5;
myTicketMachine._counter = 100;
myTicketMachine._counter = 200;

class Counter { var count = 0; func increment() { count++; } func inrementBy(amount:Int) { count += amount; } func reset() { count = 0; } func inrementBy(amount:Int, times:Int) { count += amount*times; } }

var counter1 = Counter(); counter1.increment(); counter1.increment(); counter1.inrementBy(5); counter1.reset(); counter1.inrementBy(4, times: 6);

struct Point {
    var x=0;
    var y=0;
    mutating func move(deltaX:Int, deltaY:Int) {
        x += deltaX;
        y += deltaY;
    }
}

var point1 = Point(x:4,y:5);
point1.move(19, deltaY: 35);
point1

class CPoint {
    var x=0;
    var y=0;
    func move(deltaX:Int, deltaY:Int) {
        x += deltaX;
        y += deltaY;
    }
}
var cpoint1 = Point(x:4,y:5);
cpoint1.move(19,deltaY:35);

struct Item { static var count=1; static func addOne() { count++; } init() { Item.count++; } }

var item1 = Item(); Item.addOne() Item.count Item.addOne() Item.count var item2 = Item(); Item.count

struct FiveDollarCoin {
    subscript(total:Int)->Int {
        return total/5;
    }
}

var f1 = FiveDollarCoin();
f1[200];

f1[50]

var products = ["iPhone6":100, "iPhone6+":150];

products["iPhone6"] = products["iPhone6"]!-1;

products["iPhone6+"] = products["iPhone6+"]!+1;

struct Foo { // var bar:Int=0; var bar=0; var bar2=3; var bar3=4; let bar4:Int; init() { // bar = 0; bar4=5; } }

var foo = Foo(); foo.bar = 5

class Person {
    var name:String
    init(name:String) {
        self.name = name;
        println("\(name) is being initialized");
    }
    deinit {
        println("\(name) is being destroyed");
    }

}

var ref1:Person?
var ref2:Person?
var ref3:Person?

ref1 = Person(name: "Mark")
ref2 = ref1
ref3 = ref1

println("start to set null");
ref1 = Person(name: "Mark")
ref2 = ref1
ref3 = ref1

println("start to set null");
ref1 = nil
ref2 = nil
ref3 = nil

class Car { let number:String

init(number:String) {
    self.number = number
    println("\(number) is constructed");
}
var driver:Person?
deinit {
    println("\(number) is destroyed");
}

}

var driver1:Person?
var car1:Car?
driver1 = Person(name:"Mark");
car1 = Car(number: "3688");
driver1?.ownedCar = car1
car1?.driver = driver1
car1 = nil
driver1 = nil

https://drive.google.com/file/d/0B1rMVw7ZJoGiNnpyWEgzNEQ1bEU/view?usp=sharing