본문 바로가기

::public/Swift15

Class extention struct Grade { var letter: Character var points: Double var credits: Double } class Person { var firstName: String var lastName: String init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } func printMyName() { print("My name is \(firstName) \(lastName)") } } class Student: Person { var grades: [Grade] = [] } let jay = Person(firstName: "Jay", lastName.. 2022. 11. 23.
protocol //protocol: 특정 역할을 하기 위한 메소드, 프로퍼티, 기타 요구사항 등의 정의.. protocol FirstProtocol { var name: String { get set }// 읽기, 쓰기가 동시에 가능한 프로퍼티는 { get set }을 써주어야 함 var age: Int { get }// 읽기만 가능한 프로퍼티 } protocol SecondProtocol { static var someTypeProperty: Int { get set }// 타입프로퍼티를 요구하려면 static을 선언해줘야 함 func someTypeMethod() } // 여러가지 프로토콜을 동시에 채택 가능. 프로토콜에서 요구하는 프로퍼티, 메소드를 정의해주어야함 (struct/class) ProtocolClas.. 2022. 11. 16.
Structure import UIKit struct Person { var firstName: String { // 값이 바뀌기 전 willSet { print("willSet: \(firstName) --> \(newValue)") } // 값이 바뀐 후 didSet { print("didSet: \(oldValue) --> \(firstName)") } } var lastName: String // 변수가 사용되기 전에는 메모리에 올라가지 않는다. 쓰레드에서 애러날 수 있음. lazy var isPopular: Bool = { if fullName == "Jay Park" { return true } else { return false } }() var fullName:String { get { return "\(f.. 2022. 11. 11.
Closure 함수처럼 기능을 수행하는 코드블록 함수와 다르게 이름이 없다 /* { (parameters) -> return type in statement ... } */ // Cho Simple Closure let ChoSimpleClosure = { } ChoSimpleClosure() // 코드 블록을 구현한 Closure let CodeBlockClosure = { print("this is code block") } CodeBlockClosure() // 인풋 파라미터를 받는 Closure let InputParamClosure: (String) -> Void = { name in print("input param is \(name)") } InputParamClosure("closure") // 값을 리.. 2022. 11. 7.
Set // 중복없는 유니크한 아이템을 관리할 때 사용 //var someArray: Array = [1, 2, 3, 1] var someSet: Set = [1, 2, 3, 1, 2] // {1, 2, 3} someSet.isEmpty someSet.count // 3 someSet.contains(4) // false someSet.contains(1) // true someSet.insert(5) someSet // {1, 2, 3, 5} someSet.remove(1) someSet // {2, 3, 5} 2022. 11. 7.
Dictionary var scoreDic: [String: Int] = ["Jason": 80, "Jay": 95, "Jake": 90] //var scoreDicc: Dictionary = ["jason": 80, "Jay": 95, "jake": 90] scoreDic["Jason"] // 80 scoreDic["Jake"] // 90 //scoreDic = [:] // clear scoreDic.isEmpty scoreDic.count // 기존 사용자 업데이트 scoreDic["Jason"] = 99 scoreDic // ["Jay": 95, "Jake": 90, "Jason": 99] // 사용자 추가 scoreDic["Jack"] = 100 scoreDic // ["Jason": 99, "Jake": 90, ".. 2022. 11. 6.