기초를 확실히 하고 사용하기 위해 공부용으로 포스팅!
출처들
https://baked-corn.tistory.com/26?category=718234
[Swift] Protocol [02]
Protocol Basic To Advanced 저번 포스팅에서는 프로토콜의 기본적인 개념과 문법들을 살펴보았습니다. 이번 포스팅에서는 스위프트에서 프로토콜에 대한 심화된 내용과 이를 사용하는 이유, 더 나아가 POP (Proto..
baked-corn.tistory.com
https://the-brain-of-sic2.tistory.com/37
[스위프트 : 기초] 서브스크립트 : Subscript : 지름길
안녕하세요 ! 씩이 입니다! 저는 Swift 와 iOS 를 공부하고 연구하는 대딩 ( 대학생 ) 이구요! 같은 분야를 공부하는 분들에게 조금이라도 도움이 주고 싶어서 공부하는 것들을 공유합니다. 제 3자가 있다고 가정..
the-brain-of-sic2.tistory.com
https://hcn1519.github.io/articles/2017-09/swift_escaping_closure
Swift Escaping Closure 이해하기
Swift의 Escaping Closure에 대해 알아봅니다.
hcn1519.github.io
Playground로 연습!
Protocol/escaping closure
@objc protocol ComputerScienceStudent {
var name: String { get }
var laptop: String? { get set }
func doDataStructure()
func doOperatingSystem()
func doNetwork()
@objc optional func doGraphics()
}
class Student: ComputerScienceStudent {
var _name: String = ""
var name: String {
get {
return self._name
}
set {
self._name = newValue
}
}
var _laptop: String?
var laptop: String? {
get {
return self._laptop
}
set {
self._laptop = newValue
}
}
func doDataStructure() {
print("I Love Data Structure")
}
func doOperatingSystem() {
print("It's boring")
}
func doNetwork() {
print("I like Network")
}
}
protocol Readble {
func read()
}
protocol Writeable {
func write()
}
protocol ReadWriteable: Readble, Writeable { }
protocol ReadWriteTalkable: ReadWriteable {
func talk()
}
class CanRead: Readble {
func read() {
print("I can read")
}
}
class CanWrite: Writeable {
func write() {
print("I can Write")
}
}
struct CanReadWrite: ReadWriteable {
func read() {
}
func write() {
}
}
struct CanReadWriteTalk: ReadWriteTalkable {
func talk() {
}
func read() {
}
func write() {
}
}
protocol Bird {
var name: String { get }
}
protocol Flyable {
var airspeedVelocity : Double { get }
}
struct FlappyBird: Bird, Flyable {
let name: String
let canFly: Bool
let flappyAmplitude: Double
let flappyFrequency: Double
var airspeedVelocity: Double {
return 3 * flappyAmplitude * flappyFrequency
}
}
struct Penguin: Bird {
let name: String
let canFly: Bool
}
struct SwiftbBird: Bird, Flyable {
var name: String { return "Swift\(version)"}
var version: Double
let canFly: Bool
var airspeedVelocity: Double { return version * 1 }
}
class UnknownBird: Bird, Flyable {
var name: String = ""
var airspeedVelocity = 0.0
}
extension UnknownBird {
var canFly: Bool {
if self.airspeedVelocity > 0 {
return true
}
return false
}
}
var unKnown = UnknownBird()
print(unKnown.canFly)
unKnown.name = "NailerBird"
unKnown.airspeedVelocity = 10
print(unKnown.canFly)
var completionHandlers: [() -> Void] = []
func withEscaping(completion: @escaping () -> Void) {
completionHandlers.append(completion)
}
func withoutEscaping(completion: () -> Void) {
completion()
}
class MyClass {
var x = 10
func callFunc() {
withEscaping { self.x = 100}
withoutEscaping { x = 200 }
}
}
let mc = MyClass()
mc.callFunc()
print(mc.x)
completionHandlers.first?()
print(mc.x)
typealias VoidVoidClosure = () -> Void
let firstClosure: VoidVoidClosure = {
print("Closure A")
}
let secondClosure: VoidVoidClosure = {
print("Closure B")
}
func returnOneClosure(first: @escaping VoidVoidClosure, second: @escaping VoidVoidClosure, shouldReturnClosure: Bool) -> VoidVoidClosure {
return shouldReturnClosure ? first : second
}
let returnedClosure: VoidVoidClosure = returnOneClosure(first: firstClosure, second: secondClosure, shouldReturnClosure: false)
returnedClosure()
var closures: [VoidVoidClosure] = []
func addendClosure(closure: @escaping VoidVoidClosure) {
closures.append(firstClosure)
}
func functionWithNoescapeCloosure(closure: VoidVoidClosure) {
closure()
}
func functionWithEscapeCloosure(completionHandler: @escaping VoidVoidClosure) -> VoidVoidClosure {
completionHandler
}
class SomeClass {
var x = 10
func runNoescapeClosure() {
functionWithNoescapeCloosure {
x = 200
}
}
func runEscapingClosure() -> VoidVoidClosure {
return functionWithEscapeCloosure {
self.x = 100
}
}
}
let instance = SomeClass()
let returnClosures = instance.runEscapingClosure()
returnClosures()
print(instance.x)
instance.runNoescapeClosure()
print(instance.x)
Get Set
struct Point {
var x = 0.0
var y = 0.0
}
struct Size {
var width = 0.0
var height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width/2)
let centerY = origin.y + (size.height/2)
print(centerX,centerX, "get")
return Point(x: centerX, y: centerY)
}
set(newCenter) {
print(newCenter,"set")
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
print(newCenter,"setover")
}
}
}
var center = Rect()
center.center = Point(x: 10.0, y: 10.0)
print(center.center,"center")
var square = Rect(origin: Point(x: 10.0, y: 10.0),
size: Size(width: 20.0, height: 20.0))
print(square.center,"square 1")
square.center = Point(x: 30.0, y: 30.0)
print(square.center,"sqare2")
struct Cuboid {
var width = 0.0
var height = 0.0
var depth = 0.0
var volume: Double {
return width * height * depth
}
}
let fourByFiveByTwo = Cuboid(width: 4.0, height: 4.0, depth: 4.0)
print(fourByFiveByTwo.volume)
class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("willset \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("didset \(totalSteps - oldValue)")
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 300
stepCounter.totalSteps = 500
Subscript
struct TimeTable {
let multiplier: Int
subscript(index: Int) -> Int {
return multiplier * index
}
}
let threeTimeTable = TimeTable(multiplier: 3)
print(threeTimeTable[9])
class MovieList {
private var tracks = ["like this", "killing me Dj", "Like a Prayer"]
subscript(index: Int) -> String {
get {
return self.tracks[index]
}
set {
self.tracks[index] = newValue
}
}
}
var movieList = MovieList()
print(movieList[1])
'IOS Swift > Swift 공부 기록' 카테고리의 다른 글
코드로 NavigationController 만드는법 (0) | 2020.05.25 |
---|---|
Swift) UITableViewCell에 UICollectionViewCell 넣기 (0) | 2020.04.27 |
Swift) UIStackView에 UIButton 넣기! (0) | 2020.04.16 |
IOS 개발자 면접 질문들 복습 (0) | 2020.04.08 |
Swift) Generic 기초 (0) | 2020.03.27 |