Skip to content

Latest commit

ย 

History

History
276 lines (223 loc) ยท 14.2 KB

220214_Core_Animation,_CABasicAnimation,_append,_escaping,_UITableView-Crash,_UITextViewDelegate,_typingAttributes.md

File metadata and controls

276 lines (223 loc) ยท 14.2 KB

220214 Core Animation, CABasicAnimation, append, escaping, UITableView-Crash, UITextViewDelegate, typingAttributes

TIL (Today I Learned)

2์›” 14์ผ (์›”)

ํ•™์Šต ๋‚ด์šฉ

  • ์›”์š”์ผ ํ™œ๋™ํ•™์Šต
    • ์˜ค๋‹ต๋…ธํŠธ
    • Core Animation
  • UITableView Crash ๋ฌธ์ œํ•ด๊ฒฐ
    • Thread 1: Attempted to scroll the table view to an out-of-bounds row (0) when there are only 0 rows in section 0.
    • Thread 1: Invalid update: invalid number of rows in section 0
  • UITextView - ์‹ค์‹œ๊ฐ„์œผ๋กœ ํฐํŠธ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ตฌํ˜„ํ•˜๊ธฐ

ย 

๊ณ ๋ฏผํ•œ ์  / ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

[ํ™œ๋™ํ•™์Šต ํ€ด์ฆˆ ์˜ค๋‹ต๋…ธํŠธ]

  • Arrayํƒ€์ž…์˜ append ๋ฉ”์„œ๋“œ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ํ•ญ์ƒ O(1)์ด๋‹ค.
    • ์•„๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” O(1)์ด์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์žฌํ• ๋‹นํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์ด๋‹ค.
  • SOLID ์›์น™ ๋ณต๊ธฐํ•˜๊ธฐ
    • SRP (Single responsibility principle)
      • ๋‹จ์ผ ์ฑ…์ž„ ์›์น™
      • ํ•œ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
    • OCP (Open/closed principle)
      • ๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™
      • ์†Œํ”„ํŠธ์›จ์–ด์˜ ์š”์†Œ๋Š” ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ์œผ๋‚˜ ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
    • LSP (Liskov substitution principle)
      • ๋ฆฌ์Šค์ฝ”ํ”„ ์น˜ํ™˜ ์›์น™
      • ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐ์ฒด๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์ •ํ™•์„ฑ์„ ๊นจ๋œจ๋ฆฌ์ง€ ์•Š์œผ๋ฉด์„œ ํ•˜์œ„ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
    • ISP (Interface segregation principle
      • ์ธํ„ฐํŽ˜์ด์Šค ๋ถ„๋ฆฌ ์›์น™
      • ํŠน์ • ํด๋ผ์ด์–ธํŠธ๋ฅผ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ๋ฒ”์šฉ ์ธํ„ฐํŽ˜์ด์Šค ํ•˜๋‚˜๋ณด๋‹ค ๋‚ซ๋‹ค.
    • DIP (Dependency inversion principle)
      • ์˜์กด๊ด€๊ณ„ ์—ญ์ „ ์›์น™
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” "์ถ”์ƒํ™”์— ์˜์กดํ•ด์•ผ์ง€, ๊ตฌ์ฒดํ™”์— ์˜์กดํ•˜๋ฉด ์•ˆ๋œ๋‹ค"
      • ์˜์กด์„ฑ ์ฃผ์ž…์€ ์ด ์›์น™์„ ๋”ฐ๋ฅด๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋‹ค.
  • ํƒˆ์ถœ ํด๋กœ์ €๋ฅผ ๋งŒ๋“ค ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์€?
    • @escaping () -> Void
      • ์• ๋งŒ ๋˜๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ
    • (() -> Void)?
      • ์ด๋ ‡๊ฒŒ ์˜ต์…”๋„ ํ˜•ํƒœ๊ฐ€ ๋˜๋ฉด ์ž๋™์œผ๋กœ escaping ํด๋กœ์ €๊ฐ€ ๋œ๋‹ค.

[Relationship between a view's frame and bounds]

https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html

frame,ย bounds๋ฐ center properties์€ ๋‹ค๋ฅธ property๊ณผ ๋ณ„๋„๋กœย ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,ย ํ•œ properties์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฅธ properties์— ์˜ํ–ฅ์„ ์ค€๋‹ค.

  • frame property๋ฅผ ์„ค์ •ํ•˜๋ฉดย bounds property์˜ size ๊ฐ’์ด frame rectangle์˜ new size์™€ ์ผ์น˜๋˜๋„๋ก ๋ณ€๊ฒฝ๋œ๋‹ค.ย center property์˜ ๊ฐ’์€ frame rectangle์˜ new center point๊ณผ ์ผ์น˜ํ•˜๋„๋ก ์œ ์‚ฌํ•˜๊ฒŒ ๋ณ€๊ฒฝ๋œ๋‹ค.
  • center property๋ฅผ ์„ค์ •ํ•˜๋ฉด frame์˜ ์›์  ๊ฐ’์ด ๊ทธ์— ๋”ฐ๋ผย ๋ณ€๊ฒฝ๋œ๋‹ค.
  • bounds property์˜ size๋ฅผ ์„ค์ •ํ•˜๋ฉด frame property์˜ size ๊ฐ’์ด bounds rectangle์˜ new size์™€ ์ผ์น˜ํ•˜๋„๋ก ๋ณ€๊ฒฝ๋œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ view์˜ frame์€ superView์˜ frame์— ์ž˜๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ superview์˜ frame ์™ธ๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  subviews๋Š” ์ „์ฒด์ ์œผ๋กœ ๋ Œ๋”๋ง ๋œ๋‹ค.
superview์˜ clipsToBounds property๋ฅผ true๋กœ ์„ค์ •ํ•˜๋ฉด ์ด ๋™์ž‘์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
subviews๊ฐ€ ์‹œ๊ฐ์ ์œผ๋กœ ์ž˜๋ ค์žˆ๋Š”์ง€ ์—ฌ๋ถ€์— ๊ด€๊ณ„์—†์ด touch events๋Š” ํ•ญ์ƒ target view์˜ bounds rectangle์„ ๋”ฐ๋ฅธ๋‹ค.
์ฆ‰, superview์˜ bounds rectangle ์™ธ๋ถ€์— ์žˆ๋Š” view์˜ ์ผ๋ถ€์—์„œ ๋ฐœ์ƒํ•˜๋Š” touch events๋Š” ํ•ด๋‹น view์— ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.


[Core Animation]

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/CoreAnimationBasics/CoreAnimationBasics.html#//apple_ref/doc/uid/TP40004514-CH2-SW3

  • CAAnimation
    • ๋Œ€๋ถ€๋ถ„์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ์ถ”์ƒ ํด๋ž˜์Šค
    • ์ด๊ฒŒ ์ฝ”์–ด ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์ „๋ถ€๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ฃผ๋กœ, ๊ทธ๋ฆฌ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งŽ์ด ์“ฐ๊ฒŒ ๋  ํด๋ž˜์Šค์ด๋‹ค.
    • ์ƒ์†๋ฐ›์€ ์ถ”์ƒ ํด๋ž˜์Šค
      • CAAnimationGroup
      • CAPropertyAnimation (์ถ”์ƒํด๋ž˜์Šค)
        • CABasicAnimation
        • CAKeyframeAnimation
      • CATransition

[Core Animation์ด๋ž€?]

  • ์‹œ๊ฐ์  ์š”์†Œ์— ๋Œ€ํ•ด ๊ทธ๋ž˜ํ”ฝ ๋ Œ๋”๋ง ๋ฐ ๊ตฌ์„ฑ์„ ํ†ตํ•ด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋งŒ๋“œ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ
  • ์‹œ์ž‘ ๋ฐ ๋ ํฌ์ธํŠธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ตฌํ˜„ํ•˜๋ฉด Task๊ฐ€ ์ž๋™์œผ๋กœ ์ผ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ๋“œ๋กœ์ž‰ ์ž‘์—…์„ ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ ˆ์ด์–ด ๊ฐ์ฒด๊ฐ€ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ Œ๋”๋ง ์ž‘์—…์„ ๊ฐ€์†ํ™”ํ•ด ์•ฑ ์†๋„ ๋ฐ ํ’ˆ์งˆ์˜ ๋‹ค์šด์—†์ด ๋†’์€ ํ”„๋ ˆ์ž„๊ณผ ์ž์—ฐ์Šค๋Ÿฌ์šด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋ณด์—ฌ์ค€๋‹ค.

  • ๋•Œ๋กœ๋Š” UiKit์—์„œ ์›ํ•˜๋Š” ์œ ํ˜•์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†์„ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์›ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ Core Animation์œผ๋กœ ์ง์ ‘ ๋“œ๋กญ๋‹ค์šดํ•ด์•ผ ํ•œ๋‹ค.
  • UIKit์—์„œ ์ž‘์—…ํ•˜๋“  SwiftUI์—์„œ ์ž‘์—…ํ•˜๋“  ๊ด€๊ณ„์—†์ด ๋ชจ๋“  ์• ๋‹ˆ๋ฉ”์ด์…˜์€ ๊ฒฐ๊ตญ ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ Œ๋”๋ง ๋œ๋‹ค. ์ž‘๋™ ๋ฐฉ์‹์„ ์ดํ•ดํ•œ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ์ƒ์œ„ ์ˆ˜์ค€ API๋กœ ์ž‘์—…ํ•˜๊ธฐ ๋” ์‰ฌ์›Œ์ง„๋‹ค.

  • ์ฝ”์–ด ์• ๋‹ˆ๋ฉ”์ด์…˜์€ ๋ ˆ์ด์–ด์—์„œ ์ž‘๋™ํ•œ๋‹ค.
  • ๋ทฐ์˜ ์†์„ฑ์„ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ฐ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. UIView์—๋Š” ๊ธฐ๋ณธ CALayer๊ฐ€ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹œ์ž‘ ๋ฐ ์ค‘์ง€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.
  • ๋ ˆ์ด์–ด์— ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด Core Animation์ด ๋‚˜๋จธ์ง€ ๋‘ ์ƒํƒœ ์‚ฌ์ด์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๊ฐ„ํ•˜๊ณ  ํ™”๋ฉด์—์„œ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

[CALayer]

  • ์ปจํ…์ธ ๋ฅผ ๊ด€๋ฆฌ/์กฐ์ž‘ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  • ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด๋กœ ์‰ฝ๊ฒŒ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋น„ํŠธ๋งต์œผ๋กœ ์ปจํ…์ธ ๋ฅผ ์บก์ณํ•œ๋‹ค.
  • ๋ทฐ์™€์˜ ์ฐจ์ด๋Š” ์ž์ฒด ํ˜•ํƒœ๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • ๋น„ํŠธ๋งต์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ƒํƒœ์ •๋ณด๋งŒ ๊ด€๋ฆฌ
    • ๋ ˆ์ด์–ด๊ฐ€ ์•ฑ์—์„œ ์‹ค์ œ ๋“œ๋กœ์ž‰ ์ž‘์—…์„ ํ•˜์ง„ ์•Š๋Š”๋‹ค.
    • ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๊ฐ€์ง„ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํŠธ๋ฆฌ๊ฑฐ์‹œ ๋ ˆ์ด์–ด ๋น„ํŠธ๋งต/์ƒํƒœ์ •๋ณด -> ๊ทธ๋ž˜ํ”ฝ ํ•˜๋“œ์›จ์–ด ์ „๋‹ฌ
    • UIView์— ์ตœ์†Œ 1๊ฐœ์”ฉ ์žˆ๊ณ  ๋ Œ๋”๋ง์„ ๋‹ด๋‹นํ•œ๋‹ค. (์ž์‹ ์„ ๊ทธ๋ ค์ค„ ๋ ˆ์ด์–ด)
    • 3D์— ๊ตฌ์„ฑ๋œ 2D (๋ ˆ์ด์–ด ๊ฐ์ฒด)
    • ๋ ˆ์ด์–ด๋Š” ๋‘๊ฐ€์ง€ ์œ ํ˜•์˜ ์ขŒํ‘œ๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋งŒ๋“ ๋‹ค.
      • ์  ๊ธฐ๋ฐ˜ ์ขŒํ‘œ๊ณ„
      • ๋‹จ์œ„ ๊ธฐ๋ฐ˜ ์ขŒํ‘œ๊ณ„

[Layer Tree์˜ ์ข…๋ฅ˜]

  • ์ฝ”์–ด ์• ๋‹ˆ๋ฉ”์ด์…˜์€ CALayer์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ง์ ‘ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์•„๋ž˜์™€ ๊ฐ™์€ 3๊ฐœ์˜ Layer Tree๋ฅผ ๊ฐ€์ง€๊ณ  ๊ด€๋ฆฌ
    • Model
      • ํ•ด๋‹น ๋ ˆ์ด์–ด์˜ ์‹ค์ œ ์ƒํƒœ ๊ฐ’
    • Presentation
      • ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ค‘์—๋งŒ ๊ด€๋ฆฌํ•˜๋ฉฐ ๋ทฐ์— ํ‘œ์‹œ๋˜๋Š” ํ˜„์žฌ ์ƒํƒœ๊ฐ’์„ ๋ฐ˜์˜
    • Render
      • ์‹ค์ œ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”์–ด ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ „์šฉ

[Core Animation ๋งŒ๋“ค์–ด๋ณด๊ธฐ - ์ง์„ ์œผ๋กœ ์›€์ง์ด๋Š” ๋ทฐ]

class ViewController: UIViewController {

    let redView: UIView = {
       let view = UIView(frame: CGRect(x: 20, y: 100, width: 140, height: 100))
        view.backgroundColor = .systemRed
        return view
    }()
    
    let button: UIButton = {
       let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Animate", for: .normal)
        button.titleLabel?.adjustsFontSizeToFitWidth = true
        button.backgroundColor = .systemBlue
        button.layer.cornerRadius = 8
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(redView)
        view.addSubview(button)
        button.addTarget(self, action: #selector(buttonTapped(_:)), for: .primaryActionTriggered)
        
        NSLayoutConstraint.activate([
            view.safeAreaLayoutGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: button.bottomAnchor, multiplier: 2),
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        ])
    }
    
    func animate() {
        let animation = CABasicAnimation()
        animation.keyPath = "position.x" // keyPath๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋งŒ๋“ค๊ณ  ์‹ถ์€์ง€ ์•Œ๋ฆฐ๋‹ค.
        // ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์‹œ์ž‘ ๋ฐ ์ข…๋ฃŒ์ƒํƒœ๋ฅผ ์ •์˜ํ•œ๋‹ค.
        animation.fromValue = 20 + 140 / 2
        animation.toValue = 300
        animation.duration = 1
        /*
         ์ดํ•ดํ•˜๊ธฐ ๊นŒ๋‹ค๋กœ์šด ์ ์€ shapes position ๋˜๋Š”
         anchor point์˜ ์ฝ”์–ด ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ž‘์—…์ด๋‹ค.
         position์€ ๋„ํ˜• ์ค‘๊ฐ„์ด๋‹ค.
         ๋”ฐ๋ผ์„œ rectangle์„ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋ ค๋ฉด
         ๋จผ์ € rectangle์˜ ๊ฐ€์šด๋ฐ x์  ๊ฐ’์„ ๊ณ„์‚ฐํ•œ ๋‹ค์Œ
         ์ง์‚ฌ๊ฐํ˜•์˜ ๊ฐ€์šด๋ฐ ๋งˆ์ง€๋ง‰ x ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ
         fromValue์™€ toValue๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
         */

        redView.layer.add(animation, forKey: "basic") // ๋ทฐ์˜ ๋ ˆ์ด์–ด์— ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ถ”๊ฐ€
        redView.layer.position = CGPoint(x: 300, y: 100 + 100/2) // ์ตœ์ข… ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์œ„์น˜๋ฅผ ์—…๋ฐ์ดํŠธ

        /*
         ์ด ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„๋Š” ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
         ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฐ”์™€ ๊ฐ™์ด Core Animation๋Š” ๋ชจ๋ธ ๋ ˆ์ด์–ด(ํ˜„์žฌ ์ƒํƒœ)๊ณผ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๋ ˆ์ด์–ด(์• ๋‹ˆ๋ฉ”์ด์…˜ ์ƒํƒœ)์˜ ๋‘ ๊ฐ€์ง€ ๋ ˆ์ด์–ด์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.
         ๊ทธ๋ฆฌ๊ณ  ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋๋‚˜๋ฉด final presentation state๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ model state๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
         */
    }
}

[UITableView Cell์„ selectRow๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•œ Crash]

UITableView์˜ selectRow๋ฅผ ํ†ตํ•ด Select๋ฅผ ์‹œ๋„ํ–ˆ์„ ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด์„œ Crash๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

Thread 1: 
"Attempted to scroll the table view to an out-of-bounds row (0) when there are only 0 rows in section 0. 
Table view: <UITableView: 0x13f031400; 
frame = (0 0; 420 834); 
clipsToBounds = YES; 
autoresize = W+H; gestureRecognizers = <NSArray: 0x600000031680>;
layer = <CALayer: 0x600000ec7b80>; contentOffset: {0, -74}; 
contentSize: {420, 72.5}; adjustedContentInset: {74, 0, 20, 0}; 
dataSource: <CloudNotes.MemoListViewController: 0x14880fad0>>"
  • ์ƒํ™ฉ ๋ฉ”๋ชจ์žฅ์˜ ๋งˆ์ง€๋ง‰ ๋‚จ์€ ์…€์„ ์ง€์šฐ๊ฒŒ ๋˜๋ฉด์„œ selectRow๊ฐ€ ํ˜ธ์ถœ์ด ๋˜๋Š” ์ƒํ™ฉ์ด์˜€๋‹ค.
  • ์ด์œ  ์…€์„ ์ง€์šฐ๊ณ  ๋‚œ ํ›„๋‹ˆ๊นŒ UITableView์— ๋ณด์—ฌ์ค„ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ณ , Cell๋„ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒํ™ฉ์ด์˜€๋Š”๋ฐ, ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์…€์„ Select๋ฅผ ํ•˜๋ ค๊ณ  ํ•ด์„œ ํฌ๋ž˜์‰ฌ๊ฐ€ ๋‚œ ๊ฒƒ์ด์˜€๋‹ค.
  • ํ•ด๊ฒฐ ๋”ฐ๋ผ์„œ Select๋ฅผ ํ•˜๊ธฐ ์ „์— ๋จผ์ € UITableView์— numberOfRows(inSection:) ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๊ฐ’์ด 0์ด ์•„๋‹ ๊ฒฝ์šฐ์—๋งŒ seletRow๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก guard๋ฌธ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๊ฒฐํ•ด์ฃผ์—ˆ๋‹ค.
func updateData(at index: Int) {
    guard self.tableView.numberOfRows(inSection: .zero) != .zero else {
        return
    }
    ...
    tableView.selectRow(at: IndexPath(row: .zero, section: .zero), animated: false, scrollPosition: .middle)
}

[UITableView์˜ Cell์„ deleteRows๋กœ ์ง€์› ์„ ๋•Œ ๋ฐœ์ƒํ•œ Crash]

๋ฉ”๋ชจ์žฅ์˜ ๋ฐ์ดํ„ฐ๋ฅผ JSON์œผ๋กœ ํŒŒ์‹ฑํ•œ ์ƒ˜ํ”Œ ๋ชจ๋ธ์—์„œ Core Data๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ๊ณผ์ •์—์„œ ๋‚œ ์—๋Ÿฌ์˜€๋‹ค.

Thread 1: 
"Invalid update: invalid number of rows in section 0. 
The number of rows contained in an existing section after the update (15) must be equal to the number of rows contained in that section before the update (15), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
 Table view: 
<UITableView: 0x11081d400;
 frame = (0 0; 420 1194); 
clipsToBounds = YES; 
autoresize = W+H; 
gestureRecognizers = <NSArray: 0x6000033708a0>; layer = <CALayer: 0x600003d8cb40>; 
contentOffset: {0, -74}; 
contentSize: {420, 1160}; 
adjustedContentInset: {74, 0, 20, 0};
 dataSource: <CloudNotes.NotesViewController: 0x12a808ee0>>"
  • ์ƒํ™ฉ ํ…Œ์ด๋ธ” ๋ทฐ์˜ ์„น์…˜์˜ ํ–‰ ๊ฐœ์ˆ˜์™€ ์‹ค์ œ ๋ณด์—ฌ์ค„ ์„น์…˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งž์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์ด๋‹ค.
  • ์ด์œ  ํ…Œ์ด๋ธ”๋ทฐ์˜ ์…€์„ ์‚ญ์ œํ•˜๋ฉด์„œ ํ…Œ์ด๋ธ”๋ทฐ์— ๋ณด์—ฌ์ค„ ๋ฐ์ดํ„ฐ๋„ ๋™์ผํ•˜๊ฒŒ ์‚ญ์ œ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ๋ˆ„๋ฝ๋˜์„œ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด์˜€๋‹ค.
  • ํ•ด๊ฒฐ ์…€์„ ์ถ”๊ฐ€, ์‚ญ์ œํ•  ๋•Œ ํ…Œ์ด๋ธ”๋ทฐ์— ๋ณด์—ฌ์ค„ ์„น์…˜์˜ ๊ฐœ์ˆ˜๋„ ๋™์ผํ•  ์ˆ˜ ์žˆ๋„๋ก PersistentManager์˜ notes ๊ด€๋ฆฌ(๋ฐฐ์—ด ์š”์†Œ ์ œ๊ฑฐ, ์ฝ”์–ด๋ฐ์ดํ„ฐ ์š”์†Œ ์ œ๊ฑฐ)๋„ ๋นผ๋จน์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.

[UITextView - ์‹ค์‹œ๊ฐ„์œผ๋กœ ํฐํŠธ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ตฌํ˜„ํ•˜๊ธฐ]

๋ฉ”๋ชจ์žฅ ์•ฑ ๊ตฌํ˜„ ์‹œ ๋งจ ์ฒซ์ค„์—๋Š” ํฐ ์ œ๋ชฉ์ด, ๊ทธ ์•„๋ž˜๋ถ€ํ„ฐ๋Š” ๋ณธ๋ฌธ ๊ธ€๊ผด๋กœ ์„ค์ •ํ•˜์—ฌ ์ œ๋ชฉ๊ณผ ๋‚ด์šฉ์„ ๊ตฌ๋ถ„ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ธ์ง€ ์ฐพ์•„๋ณด์•˜๋‹ค.

UITextViewDelegate - textView(_:shouldChangeTextIn:replacementText:) ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•ด๋ณด์•˜๋‹ค.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let currentText = textView.text as NSString // ํƒ€์ดํ•‘ ํ›„ ํ…์ŠคํŠธ
    let titleRange = currentText.range(of: Constant.lineBreak.description) // title์˜ range๋ฅผ linebreak๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ•œ๋‹ค.
    if titleRange.location < range.location { // title์˜ location๊ณผ ์ „์ฒด ํ…์ŠคํŠธ์˜ location์„ ๋น„๊ต
        textView.typingAttributes = Constant.bodyAttributes // title๋ณด๋‹ค ์ „์ฒด ํ…์ŠคํŠธ์˜ location์ด ํฌ๋‹ค๋ฉด
    } else {
        textView.typingAttributes = Constant.headerAttributes // title์˜ location์ด ์ „์ฒด ํ…์ŠคํŠธ๋ณด๋‹ค ํฌ๋‹ค๋ฉด
    }
    return true
}

๏ฟฝ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๋งˆ๋‹ค title์˜ location๊ณผ ์ „์ฒด ํ…์ŠคํŠธ์˜ location์„ ๋น„๊ตํ•˜์—ฌ typingAttributes๋ฅผ ํ• ๋‹นํ•ด์ฃผ๋„๋ก ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.