iOS-Develop(Swift)2017.01.05 12:29


1.

var product = [String:[String]]()

product = ["it":["phone","computer"],"school":["pen","note"]]

product["it"]?.append("tablet")


print(product

//["it": ["phone", "computer", "tablet"], "school": ["pen", "note"]]


product.updateValue(["handle"], forKey: "car")


print(product)  

//["it": ["phone", "computer", "tablet"], "car": ["handle"], "school": ["pen", "note"]


2.

var product2 = [String:[String:Any]]()

product2 = ["it":["phone":4],"school":["pen":2,"note":3]]


product2["it"]?.updateValue(1, forKey: "computer")  //it에 추가적으로 값을 넣는다.

print(product2)

["it": ["phone": 4, "computer": 1], "school": ["pen": 2, "note": 3]]


product2.updateValue(["android":1], forKey: "it") //it에 기존값을 제거하고 새롭게 넣는다.

print(product2)

["it": ["android": 1], "school": ["pen": 2, "note": 3]]


product2.updateValue(["handle":1,"gear":1], forKey: "car")  //car 값을 새롭게 넣는다.

print(product2)

["it": ["android": 1], "car": ["gear": 1, "handle": 1], "school": ["pen": 2, "note": 3]]


3.

var product2 = [String:[String:Any]]()

product2 = ["it": ["phone": 4, "computer": 1], "car": ["gear": 1, "handle": 1], "school": ["pen": 2, "note": 3]]


for name in product2.keys {

    print("My first key is \(name)") //1번째 key값을 가져온다.

     //My first key is it

     //My first key is car

    //My first key is school


    if let second = product2[name] {

        for (key, value) in second {

                       print("My Second key is \(key)")  //2번째 key값을 가져온다.

            //My Second key is phone

            //My Second key is computer

            //My Second key is handle

            //My Second key is gear

            //My Second key is pen

            //My Second key is note

            print("My value is \(value)") //value를 가져온다.

            //My value is 4

            //My value is 1

            //My value is 1

            //My value is 1

            //My value is 2

            //My value is 3

        }

    }

}

- 결과값

My first key is it

My Second key is phone

My value is 4

My Second key is computer

My value is 1

My first key is car

My Second key is handle

My value is 1

My Second key is gear

My value is 1

My first key is school

My Second key is pen

My value is 2

My Second key is note

My value is 3 


                      
Posted by 꼴찌생각