ios

    [swift_09]๊ตฌ์กฐ์ฒด

    [swift_09]๊ตฌ์กฐ์ฒด

    ๊ตฌ์กฐ์ฒด ๊ตฌ์กฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ ์ด์œ  var name = ["Park","Choi","Kim","Lee"] var age = [3,4,5,6] var height = [40,50,60,70] ํ•œ ์ธ๋ฌผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด ๋„ฃ์œผ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ณ , ๋ฐฐ์—ด ๊ฐ๊ฐ์„ ์ผ์ผํžˆ ์จ์•ผํ•˜๊ธฐ์— ์–ด๋ ค์›€ ๊ตฌ์กฐ์ฒด ์„ ์–ธ struct Student{ var name: String var age: Int var height: Int } ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ตฌ์กฐ์ฒด ์ƒ์„ฑ var student1 = Student(name: "Park", age: 3, height: 40) var student2 = Student(name: "Choi", age: 4, height: 50) ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์œ„ ์ฒ˜๋Ÿผ ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ..

    [swift_08]ํด๋ž˜์Šค

    [swift_08]ํด๋ž˜์Šค

    ํด๋ž˜์Šค Class ์„ ์–ธ class Vehicle{ var currentSpeed = 0.0 var description:String { return "Traveling at \(currentSpeed) miles per hour"; } func makeNoise(){ print("noiseless") } } ํด๋ž˜์Šค ์„ ์–ธ Class ์‚ฌ์šฉ let someVehicle = Vehicle() someVehicle.currentSpeed = 1.0 print(someVehicle.currentSpeed) someVehicle.makeNoise() print(someVehicle.description) class ์ƒ์† class Tandem:Bicycle{ var currentNumberOfPassengers = ..

    [swift_07]ํ•จ์ˆ˜

    [swift_07]ํ•จ์ˆ˜

    ํ•จ์ˆ˜ voidํ˜• ํ•จ์ˆ˜ func hello(){ print("Hello") } ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…์ด ์—†์Œ Return func hello2() -> String{ return "Say Hello" } ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž… : String Parameter func add(a:Int, b:Int) -> Int{ return a + b } add(a:3 , b:4) parameter์™€ argument label์ด ๋™์ผ Default parameter func add2(a:Int = 3, b:Int) -> Int{ return a + b } a ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ธฐ๋ณธ๊ฐ’ 3, ๋งŒ์•ฝ argument a์— ๋Œ€์ž…์ด ์—†์œผ๋ฉด a = 3 Argument Label func add3(a first:Int, _ second:Int) -> Int{..

    [swift_06]์กฐ๊ฑด๋ฌธ if๋ฌธ๊ณผ switch๋ฌธ

    [swift_06]์กฐ๊ฑด๋ฌธ if๋ฌธ๊ณผ switch๋ฌธ

    if๋ฌธ if๋ฌธ ์„ ์–ธ๊ณผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• let age = 7 if age = 3 && age < 20{ print("Child") } else{ print("Adult") } ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” if๋ฌธ๊ณผ ๋™์ผํ•จ Switch๋ฌธ switch๋ฌธ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• switch age { case 0,1,2 : print("Baby") case 3...19 : print("Child") default : print("Adult") } switch๋ฌธ์€ ๊ธฐ์กด์— ๋‚ด๊ฐ€ ์•Œ๊ณ  ์žˆ๋˜ switch์— ๋น„ํ•ด ์ƒ๋‹นํžˆ ๊ฐ•๋ ฅํ•จ. ,๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์กฐ๊ฑด์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ. where์ ˆ์„ ํ™œ์šฉํ•˜์—ฌ ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ์Œ break๋ฌธ์ด ํ•„์š” ์—†์Œ

    [swift_05]๋ฐ˜๋ณต๋ฌธ while๊ณผ for๋ฌธ

    [swift_05]๋ฐ˜๋ณต๋ฌธ while๊ณผ for๋ฌธ

    while๋ฌธ while๋ฌธ ์„ ์–ธ๊ณผ ์‚ฌ์šฉ var index = 5 while index < 0{ index -= 1 print(index) } For๋ฌธ ๋ฐฐ์—ด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ• let names = ["a","b","c"] for name in names { print(name) } python์˜ for-in ๋™์ผ ์‚ฌ์ „์—์„œ ํ™œ์šฉํ•˜๋Š” let a = "a" let b = " b" var first = a + b var second = "c" second += first // ca b ์ˆซ์ž ๋ฒ”์œ„๋กœ ์‚ฌ์šฉํ•˜๊ธฐ for index in 1...5{ print("\(index) times 5 is \(index * 5)") } for _ in 1...5{ print("Hello") } stride ํ™œ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ let m..

    [swift_04]Collection-Dictionary

    [swift_04]Collection-Dictionary

    Dictionary Mutable Dictionary ์ƒ์„ฑ var dictionary = Dictionary() var dictionary2 = [String:Int]() Dictionary์— ์ถ”๊ฐ€ dictionary2["and"] = 6 dictionary2["snake"] = 0 []๊ด„ํ˜ธ ์•ˆ์— key, ๊ทธ๋ฆฌ๊ณ  assignment๋’ค์— value Dictionary ์ดˆ๊ธฐํ™” dictionary3 = ["and":6,"snake":0,"cat":4] :์„ ํ™œ์šฉํ•˜์—ฌ key์™€ value ๊ตฌ๋ถ„ํ•˜์—ฌ ์ƒ์„ฑ value ๋ณ€๊ฒฝ dictionary3["cat"] = 5 ๊ธฐ์กด์— ์žˆ๋Š” key๊ฐ’์— ์ƒˆ๋กœ์šด ๊ฐ’์„ assign immutable Dictionary ์ƒ์„ฑ let dictionary4 = ["ant":3,"snake":0..

    [swift_03]Collection-Array

    [swift_03]Collection-Array

    Array Mutable ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ• var emptyArray = Array() var emptyArray2 = [String]() ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•1(append) emptyArray2.append("Anna") emptyArray2.append("Alex") ๋ฐฐ์—ด์˜ ๋’ค์ชฝ์œผ๋กœ ๊ณ„์†ํ•ด์„œ ์ถ”๊ฐ€๋จ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•2(+=) var array3 = ["A","B","C","D"] array3 += ["E"] array3 += ["F","G"] ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•3(๋™์‹œ์— ๋ณ€๊ฒฝ) array3[3...5] = ["a","b","c"] index 3,4,5๋ฅผ ๊ฐ๊ฐ์œผ๋กœ ๋ณ€๊ฒฝ ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ• array3[3] ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ ‘๊ทผ(0๋ถ€ํ„ฐ ์ธ๋ฑ์Šค ์‹œ์ž‘) ๋ฐฐ์—ด์˜ ํŠน์ • ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๋Š” ๋ฒ• array..

    [swift_02]String

    [swift_02]String

    String ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ var str = """ A is first "B is second" C is third """ \์—†์ด ๊ฐœํ–‰๊ณผ ํŠน์ˆ˜๋ฌธ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅ empty ๋ฌธ์ž์—ด var empty1 = "" var empty2 = String() if empty1.isEmpty{ // if empty1 is empty string } isEmpty : ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด true, ์•„๋‹ˆ๋ฉด false ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ let a = "a" let b = " b" var first = a + b var second = "c" second += first // ca b +์—ฐ์‚ฐ์ž ํ™œ์šฉํ•ด ํ•ฉ์น˜๊ธฐ ๊ฐ€๋Šฅ, += ์—ฐ์‚ฐ๋„ ๊ฐ€๋Šฅ ์—ฐ์‚ฐ ์‹œ ๋’ค์— ๋ถ™์Œ

    [swift_01]๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜ ์„ ์–ธ

    [swift_01]๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜ ์„ ์–ธ

    ๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜ ์„ ์–ธ ๋ณ€์ˆ˜(variable) ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ• var hello = "hello" var world : String = "world!" print(type(of:a)) // String print(type(of:b)) // String ์ƒ์ˆ˜(constant) ์ƒ์ˆ˜๋Š” ๋ณ€ํ•  ์ˆ˜ ์—†๋Š” ๊ฐ’. ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ• let hello = 1 let hello : int = 2 String ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ var str = """ A is first "B is second" C is third """ \์—†์ด ๊ฐœํ–‰๊ณผ ํŠน์ˆ˜๋ฌธ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅ empty ๋ฌธ์ž์—ด var empty1 = "" var empty2 = String() if empty1.isEmpty{ // if empty1 i..