Swift 탐구 생활

사수님께 배운 내용, RayWenderlich DevCon을 통해 학습한 내용, 구글링한 내용 등 - 각종 문제 상황을 해결하면서 남겨진 스위프트 지식들을 기록하는 포스트 입니다. 비정기적으로 새로운 내용을 학습할 경우 업데이트 됩니다.

01. Memory Leak





02. Frame과 Bounds


03. Copy와 DeepCopy


04. 스크롤 퍼포먼스


05. AutoLayout 적용 시점


06. init rect & coder

collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "MyIdentifier", for: indexPath) 


07. Xcode config


08. ScrollView


09. Data에 didSet을 통해 UI를 변경하기

var separatorsVisible: Bool = false {
  didSet {
    horizontalSeparator.isHidden = !separatorsVisible
    verticalSeparator.isHidden = !separatorsVisible
  }
}


10. 오토레이아웃 우선순위를 이용하는 애니메이션 트릭 (Lyft)

func setTextLabel(_ textData: String?, animated: Bool = true) {
  label.text = textData
  labelContainer.layoutIfNeeded()
  labelWidth.isActive = textData != nil
  UIView.animate(withDuration: animated ? 0.25 : 0, animations: layoutIfNeeded)
}


11. Machine Learning in iOS


12. iOS Concurrency

DispatchGroup

extension UIView {
  static func animate(withDuration duration: TimeInterval, animations: @escaping () -> Void, group: DispatchGroup, completion: ((Bool) -> Void)?) {
    group.enter()
    animate(withDuration: duration, animations: animations) { (success) in
      completion?(success)
      group.leave()
    }
  }
}
animationGroup.notify(queue: DispatchQueue.main) {
  //Do Something
}

Race Condition

let workerQueue = DispatchQueue(label: "a", attributes: .concurrent)
let group = DispatchGroup()

ThreadSafe with DispatchBarrier

let isolationQueue = DispatchQueue(label: "isolation", attributes: .concurrent)

override func changeProperty(first: String, second: String) {
    isolationQueue.async(flags: .barrier) {
        super.changeName(first: firstName, second: lastName)
    }
}
    
override var first: String {
    return isolationQueue.sync {
        return super.first
    }
}


13. Reconstructing Popular iOS Animations


14. Server Side Swift with Vapor


15. 콜렉션뷰 Cell의 ContentView

// Example
self.addConstraint(self and contentView top)
self.addConstraint(self and contentView bottom)
self.addConstraint(self and contentView left)
self.addConstraint(self and contentView right)

16. OptionSet, 비트 연산

struct ShadowDirection: OptionSet {
    let rawValue: Int
    static let left  = ShadowDirection(rawValue: 1 << 0)
    static let right = ShadowDirection(rawValue: 1 << 1)
    static let up  = ShadowDirection(rawValue: 1 << 2)
    static let down  = ShadowDirection(rawValue: 1 << 3)
    static let all: ShadowDirection = [.left, .right, .up, .down]
}

17. KVO

//Example
scrollView.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentOffset), options: [.new, .old], context: &OffsetContext)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
}

18. 자료형과 버스 크기

19. Accessibility

20. inline function

21. Animation

22. Extra

//Stackoverflow
//https://stackoverflow.com/questions/17041669/creating-a-blurring-overlay-view
//only apply the blur if the user hasn't disabled transparency effects

if !UIAccessibilityIsReduceTransparencyEnabled() {
    view.backgroundColor = .clear

    let blurEffect = UIBlurEffect(style: .dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    //always fill the view
    blurEffectView.frame = self.view.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
} else {
    view.backgroundColor = .black
}

23. Layout guide