2016年7月17日 星期日

iOS Session

lesson 1,2

2016/7/14, 2016/7/15
(10hours)

Github : https://github.com/jicianho/XCode-Tutorial

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

var question:Int = 23 ; var q1:Int = 4

var emotion:String = " 😂"
//Edit -> emoji
var name:String = "James"
Int.max
print("people \(name) \(question) emotion is \(emotion) ")

if emotion.isEmpty{
    print (" the string was emptied")
}else{
    print (emotion+name)
}

//str can addtion by "+" or ".write"
name.write("ho")


// String of SWIFT is value type
var newStr:String = name
newStr.write("Hello")

if name.hasPrefix("Ja"){
    print("yes")
}

// uppercased let String uppercase, is a new memory space(orginal string still here)
name.uppercased()


name.characters.count

//Constant, Tuple, Optional
//常數 多樣數 可能沒有數

//"let" will let var be constant
let month = 12

var number = 1
let fab = number*2

var strQuestion = "23"
//Int? is mean maybe Int, so it's type Int?, not Int ,if "strQuestion" isn't int, pulse nil
var intques:Int? = Int(strQuestion)
// after Int? , should use "!", ! is warpped

if intques != nil{
    var ques:Int = intques!
}
// as same as
if let value = intques{
    var ques = value
}


// ? is convert, ! is convert "by force"
var name2:String? = " Bucky "
name2?.write("roberts")

var name3:String? = " Bucky "
name3!.write("roberts")

// all name4 is name4!
var name4:String! = " Bucky "
name4.write("roberts")



//-- Tuple --

var person:(String, Int) = ("Jane", 23)
person.0
person.1

var car:(vendor:String, year:Int) = ("Honda",1999)
car.vendor
car.year

var (x,y):(Int, Int) = (100,20)
x
y



var numa4r:Int? = Int(arc4random_uniform(33445555))

var intNuma4r:Int = numa4r!

if (intNuma4r % 11 == 0) && (intNuma4r % 13 == 0) {
    print("Find it!")
}


//-- function --

func abGame(fguess:Int ) -> (fa:Int,fb:Int) {
    let fquestion:Int = 23
    let fq1:Int = fquestion / 10 ; let fq2:Int = fquestion % 10
    let fg1:Int = fguess / 10
    let fg2:Int = fguess % 10
    var fa:Int = 0 ; if fq1 == fg1 {
        fa=fa+1
    }
    if fq2 == fg2 {
        fa=fa+1 }
    var fb:Int = 0
 
    if fq1 == fg2 {
        fb+=1 }
 
    if fq2 == fg1 {
        fb+=1
    }
    print("\(fa) A \(fb) B")
    return (fa,fb)
}


var r = abGame(fguess:23)
abGame(fguess:02)
r.0
r.1

func sum(x1:Int, x2:Int) -> Int{
    return x1+x2
}

var sum_num = sum(x1:10,x2:20)



func test(arg:Int){
    "Test Int"
}
func test(arg:Float){
    "Test Float 123"
}
func test(arg:Double){
    "Test Double 123"
}

//test(6.0)  // which function is called




var count = 6
while count > 0{
    print("**")
    count -= 1
}

//"for-in" new write method
for index in 1..<6  {
    print("*\(index)*")
}

//same as "do-while"
repeat {
    print("*****")
    count = count - 1
} while count>0



var input: String = "4321"
for char:Character in input.characters {
    var str = "\(char)"
    var num = Int(str)!
    print("\(num*2)")
}


func password_check(pasw:String){
    var num_exist:Bool = false
    var upcase_exist:Bool = false
    var lowcase_exist:Bool = false
    var over_count:Int = 0
 
    for char:Character in pasw.characters {
        let str = "\(char)"
        let num:Int? = Int(str)
     
        if num != nil {
            num_exist = true
        }
     
        if str.uppercased() == str {
            upcase_exist = true
        }
     
        if str.lowercased() == str {
            lowcase_exist = true
        }
     
        over_count += 1
    }
    if !num_exist || !upcase_exist || !lowcase_exist || over_count >= 8 {
        print("failure")
    }else{
        print("OK!")
    }
}

password_check(pasw: "s")

//-- collecttion --
// is value type

var names_1:[String] = ["Michael", "James", "Robert"]
var names_2:Array<String> = ["Michael", "James", "Robert"]


names_1.count
if names_1.isEmpty {
    "Empty"
}else {
    "Not empty"
}

names_1[1] = "Allen"
names_1

names_2[0..<2] = ["Scent","Scent"]
names_2

names_2 += ["Bucky"]

names_2.insert("Tom", at: 1)
names_2.remove(at: 2)


var cars:[String] = ["Toyota","BMW"]

cars[1] = "Mercedes"

var vars2 = cars


for (index, object) in cars.enumerated() {
    print("index \(index) is \(object)")
}

var ages = Array<Double>(repeating:2.0, count:3)

var name2_sum = 0
for (index, object) in names_2.enumerated() {
    name2_sum += object.characters.count
    print("\(name2_sum)")
}
print("\(name2_sum)")


var addressBook:Dictionary<String, String> = ["Michael":"0912345678","James":"0988776655", "Robert":"0922334455" ]

//var addressBook:[String:String] = ["Michael":"0912345678","James":"0988776655", "Robert":"0922334455" ]

addressBook.count

addressBook.updateValue("0912345678", forKey: "Michael")
addressBook
addressBook.updateValue("0912345678", forKey: "Bucky")
addressBook.keys
addressBook.values

var dict:[String:Int] = ["Michael": Int(arc4random_uniform(30)),
                         "James":Int(arc4random_uniform(30)),
                         "Bucky":Int(arc4random_uniform(30)),
                         "Jeanifer":Int(arc4random_uniform(30)),
                         "Yuma":Int(arc4random_uniform(30))]

//array has many dictionry
//watch out type define of array1 and array2
var newArray:[[String:Int]] = []
var newArray2:[(String,Int)] = []

for (name,age) in dict {
    if age < 18 {
        newArray2.append((name,age))
        newArray.append([name:age])
        print("\(name),\(age)")
    }
}

print("\(newArray)")
print("\(newArray2)")



//--- nsarray ----
var address:[String] = ["1","2"]
//NSMutableArray is immutable array
var nsaddress:NSMutableArray = NSMutableArray()
nsaddress.add("Michael")
print("\(nsaddress)")

var nsAddress2 = nsaddress

nsAddress2.add("James")

print("\(address)")
print("\(nsAddress2)")



//--- struct ---
struct PlayGround {
    var question = 23
    func randomQuestion(){
    }
    func abGame(guess:Int) -> (a:Int, b:Int) {
        return (0,0)
    }
}


var play:PlayGround = PlayGround()
// if var question is has not inital, must give it
//var play:PlayGround = PlayGround(question:9)
//or play.question = 45

//let play2:PlayGround = PlayGround()
//play2.question = 33




// --- lottery picking balls ---
//mutating <--> self (like "this")
struct PlayGround_mutating {
    var question:Int
    //init is SWIFT's Constructor, like func but without word "func"
    init(){
        question = 23
        randomQuestion()
    }
    mutating func randomQuestion(){
     
        let num = 2
        var balls:[Int] = [Int](repeating: 9, count: 10 )
        for index in 0..<balls.count {
            balls[index] = index
        }
     
        var questions = [Int](repeating: 10, count: num )
        for count:Int in 0..<num {
            let uValue = UInt32(count)
            //every times sub one ball
            let selectedIndex:UInt32 = arc4random_uniform(10 - uValue)
            let intIndex:Int = Int(selectedIndex)
            questions[count] = balls[intIndex]
            balls.remove(at:intIndex)
            print(questions)
        }
        self.question = questions[0] * 10 + questions[1]
    }
}

//Three define method of array
//var names:[String] = Array<String> or () or [String]

var playg:PlayGround_mutating = PlayGround_mutating()
playg.randomQuestion()

//Struct is value type
//all value will be copied when assignment
var play1:PlayGround = PlayGround()
play1.question = 67
var play2:PlayGround = play1
play2.question = 23

play1.question
play2.question


//--- Object ---
var components:NSURLComponents? =  NSURLComponents(string:"https:www.google.com.tw")

components?.scheme = "http"
components?.scheme
//instance is means it's have memory spaces


var newURL = NSURL(string: "http://www.apple.com")
newURL!

var formatter:DateFormatter = DateFormatter()
var date = NSDate()
formatter.dateFormat = "YYYY-MMM-dd GGG hh:mm:ss"
formatter.string(from:date as Date)


//--- Custom Class ---
//(use class complier)



//--- Inheritance ---
//(use class complier)



沒有留言:

張貼留言