패스트캠퍼스) ios 개발 챌린지

[패스트캠퍼스 수강 후기] IOS개발강의 100% 환급 챌린지 19회차 미션

student513 2020. 11. 20. 17:43

dictionary

var scoreDic: [String: Int] = ["Jason": 80, "Jay": 95, "Jake": 90]
var scoreDic : Dictionary<String, Int> = ["Jason":80, "Jay" : 95, "Jake" : 90]

scoreDic["Jason"] // 80
scoreDic["Jerry"] // nil. optional하게 처리된다

// 안정적인 optional 처리 방법
if let score = scoreDic["Jason"]{
	score
}
else {
	//.. score 없음
}



scoreDic = [:] //clear
scoreDic.isEmpty //비어있는지
scoreDic.count //원소가 몇 개인지


scoreDic["Jason"] = 99 // value update
scoreDic["Jack"] = 100 // push element 
scoreDic["Jack"] = nil // remove element


// for loop
for (name, score) in scoreDic {
	print("\(name), \(score)")
}

for key in scoreDic.keys {
	print(key)
}




// 1 이름, 직업, 도시에 대해서 본인의 딕셔너리 만들기
// 2 여기서 도시를 부산으로 업데이트 하기
// 3 딕셔너리를 받아서 이름과 도시 프린트하는 함수 만들기

var myDic : [String : String ] = ["name":"jo", "job":"student", "city":"seoul"]
myDic["city"] = "busan"
func myfunc(dic : [String:String] ) {
	if let name = dic["name"], let city = dic["city"]{
		print(name, city)
	}
	else {
		print("no")
	}
}

myfunc(dic: myDic)


Set

// Set: 중복이 없는 item의 집합

var someSet: Set<Int> = [1, 2, 3 ,1] // {2, 3, 1}

someSet.isEmpty
someSet.count

someSet.contains(4) //false
someSet.contains(1) //true

someSet.insert(5) // {2, 3, 1, 5}
someSet.remove(1) // {2, 3, 5}

 

Closure

// Closure: 이름이 없는 메소드
var multiplyClosure : (Int, Int)->Int = {
	(a: Int, b: Int) -> Int in
	return a * b
}

var multiplyClosure : (Int, Int)->Int = {
	a, b in
	return a * b
}

var multiplyClosure : (Int, Int)->Int = {$0 * $1}

let result = multiplyClosure(4,2) // 8


func operateTwoNum(a : Int, b : Int, operation : (Int, Int) -> Int) -> Int {
	let result = operation(a, b)
	return result
}

operateTowNum(a: 4, b: 2, operation: multiplyClosure)

var addClosure: (Int, Int) -> Int = { a, b in
	return a + b
}

operateTwoNum(a: 4, b: 2, operation: addClosure)

// 즉석에서 closure 함수 선언 가능
operateTwoNum(a: 4, b: 2) {a, b in
	return a / b
}

 

올인원 패키지 : iOS 앱 개발👉https://bit.ly/2FjWizq