반응형

개발자 코드(Code)/Swift(문법) 13

Swift) 13_Closure

import Cocoa // Closure // first-citizen(일급 시민) // 변수, 상수 등으로 저장, 전달인자로 전달이 가능 // 함수는 이름이 있는 Closure다 // 함수보다 먼저 만들어진 것 // 써도 되고 안써도 되는 것 // 파이썬의 Lambda 함수와 비슷하다. // alert 띄울 때 무조건 써야하는 것 // 기능이 있는 변수 // ----------------------------- // Closure와 함수의 비교 // ----------------------------- // 함수 func sumFunction(a: Int, b: Int) -> Int{ return(a + b) } var sumResult: Int = sumFunction(a: 50, b: 20) pr..

Swift) 11_클래스

import Cocoa //Class // 구조체 타입을 정하는 것이다. class Sample { var sampleProperty: Int = 10 // 가변 Property let fixedSampleProperty: Int = 100 // 불편 Property static var typeProperty: Int = 1000 // 타입 Property func instanceMethod() { print("instance method") } //타입 메서드 // 재정의 불가 타입 메서드 - static static func typeMethod() { print("type method-static") } // 재정의 기능 타입 메서드 - class class func classmethod(){ pri..

Swift) 10_Struct

import Cocoa // 10_ Struct // 구조체 // 구조체는 Swift에서 Type을 정의한다. // kotlin은 Struct구조이다. // 2020. 구글스토어 70% 코틀린이다. // Property & Method struct Sample { var sampleProperty: Int = 10 // 가변 Property let fixedSampleProperty: Int = 100 // 불편 Property static var typeProperty: Int = 1000 // 타입 Property func instanceMethod() { print("instance method") } static func typeMethod(){ print("type method") } } // ..

Swift) 09_값타입과 참조타입

import Cocoa // 09_ 값타입과 참조타입 p.20 /* @ Class - 전통적인 OOP 관점에서의 클래스 - 단일 상속 - 메소드 - 프로퍼티 - 참조타입 @ @ Struct (스위프트는 Struct언어이다.) - 다양한 형태로 사용 가능 - 상속 불가 - 메소드 - 프로퍼티 - 값 타입 - 구조체의 사용 시기 - 연관된 값들을 모아서 하나의 데이터 타입으로 선언하고 싶을 때 - 다른 객체 또는 함수등으로 전달 될 때 - 참조가 아닌 복사를 원할 때 - 자신을 상속할 필요가 없거나 상속 받을 필요가 없을 때 @ Enum (열거형) - 상속 불가 - 메소드 - 프로퍼티 - 값 타입 - 유사한 종류의 여러 값을 유의미한 이름으로 한 곳에 모아 정리 @ Value와 Reference 차이 - V..

Swift) 08_옵셔널.Playground

import Cocoa // Optional // - 값이 있을 수도 있고, 없으 수도 있음 // - nil을 포함 할 수도 있고, 아닐 수 도 있다. var constantNum = 100 //constantNum = nil // let optionalConstant : Int! = nil // 가져올 때 ! 쓴다. let optionalConstant2: Int? = nil // 선언할 때 ? 쓴다. // Optaional 계산 1 var constantNum1: Int? = 100 print(constantNum1! + 10) // Nil 값에 더하기는 안된다. constantNum1 = nil //print(constantNum1! +1) // Nil값 switch로 확인 방법 // 데이터 있을 ..

Swift) 07_반복문.Playground

import Cocoa // ----------------------------------------------------------------------- // let names = ["Anna","Alex","Brian","Jack"] for name in names{ print("Hello, \(name)!") } for (index, text) in names.enumerated(){ print("The name at index \(index) is \(text)") } // 얼마만큼 증가하는가? 0 부터 60까지 5만큼 증가한다. let minutes = 60 let minuteInterval = 5 for tickMark in stride(from: 0, to: minutes, by: minu..

Swift 06.컬렉션 set집합연산

// Set : 집합연산 let setA: Set = [1,2,3,7,4] let setB: Set = [5,3,2,8,9] //print(setA.union(setB)) // 현재 랜덤 값 let unionSet = setA.union(setB) print(unionSet.sorted()) //정렬해서 출력하기 print(setA.intersection(setB)) // 교집합 print(setA.subtracting(setB)) // 차집합 //Set의 멤버십과 동등 비교 let houseAnimals: Set = ["dog","cat"] let farmAnimals: Set = ["cow","chicken","sheep","dog","cat"] let cityAnimals: Set = ["duck"..

Swift 05. 컬렉션 Dictionaray

// Dictionary // key가 String이고 value 가 String인 빈 Dictionary생성 var stringDictionary: Dictionary = [String:String]() // [:] 는 비어있다는 표시 stringDictionary["name"] = "유비" print(stringDictionary) stringDictionary["name1"] = "관우" stringDictionary["name2"] = "장비" print(stringDictionary) print(stringDictionary["name"]!) //Optional을 없애기 위한 방법 ! 느낌표가 뜨고 난 뒤 optional없어짐 // -----------------------------------..

Swift 04.컬렉션 Aarry

import Cocoa /* 컬렉션 : 여러 값들을 묶어서 하나의 변수로 사용 Array : 순서가 있는 리스트 컬렉션 Dictionary : 키와 값의 쌍으로 이루어진 컬렉션 Set : 순서가 없고 멤버가 유일한 컬렉션, 집합 연산 */ // Array // 빈 Int Array 생성 // 제네릭 var intVariable: Array = Array() intVariable.append(1) intVariable.append(10) intVariable.append(100) print(intVariable) print(intVariable.contains(100)) print(intVariable.contains(99)) print(intVariable[0]) print(intVariable[0.....

반응형