ukSeung iOS

[Swift] print와 dump차이, 콘솔에 로그찍기 Console Log 본문

iOS/Swift

[Swift] print와 dump차이, 콘솔에 로그찍기 Console Log

욱승 2023. 3. 16. 15:17

안녕하세요 욱승입니다.

이번 포스팅에선 콘솔에 로그 찍는 방법을 알아보겠읍니다

넝담 ㅋ

 

 

'print', 'dump'디버깅 목적으로 사용되는 Swift의 내장 함수입니다. 

Swift에서 콘솔에 로그를 출력하는 방법은 두가지가 있습니다. 

print

dump

 

print vs dump

 

print - 콘솔 출력에 변수, 상수 및 표현식의 값을 인쇄하는 함수입니다. 디버깅 목적으로 콘솔에 메시지를 표시하는 간단하고 빠른 방법입니다.

dump - 개체의 구조와 내용에 대한 자세한 설명을 인쇄합니다. 클래스, 구조체 및 배열과 같은 복잡한 개체를 검사하는 데 사용할 수 있습니다.

 

예제1

import Foundation
import UIKit


final class GameCenterViewController: UIViewController, ViewAttribute {
 
    var dic : Dictionary<String, String> = ["a": "1", "b": "2"]
       
    override func viewDidLoad() {
        
        print(dic)
        dump(dic)
    }
}

결과 값

Dictionary를 나타낸 로그 첫번째라인만 print, 나머지는 dump

dump가 더 디테일하게 나오쥬?

 

예제2

import Foundation
import UIKit

struct Person {
    var age = 28
    var name = "욱승"
}

final class GameCenterViewController: UIViewController, ViewAttribute {
    let person = Person()
    
    override func viewDidLoad() {
        
        print(person)
        dump(person)
    }
}

 

첫번째 라인이 print, 두번째 라인부터 dump

 

?ㅋㅋ

 

+ 응용

import Foundation

internal func traceLog(_ description: String,
           fileName: String = #file,
           lineNumber: Int = #line,
           functionName: String = #function) {

    let traceString = "\(fileName.components(separatedBy: "/").last!) -> \(functionName) -> \(description) (line: \(lineNumber))"
    print(traceString)
}

이런식으로 전역 함수하나 정의해주시고

호출은 traceLog("{아무거나}")

 

//
//  GameCenterViewController.swift
//  HowFastYouReact
//
//  Created by plsystems on 2023/03/16.
//

import Foundation
import UIKit

final class GameCenterViewController: UIViewController, ViewAttribute {
    
    override func viewWillAppear(_ animated: Bool) {
        traceLog("5")
        
        self.navigationController?.isNavigationBarHidden = false
    }
    
    override func viewDidLoad() {
        traceLog("1")
        
        setUI()
        setAttributes()
        setUpControl()
    }
    
    func setUI() {
        
        self.view.backgroundColor = .white
        traceLog("2")
    }
    
    func setAttributes() {
        traceLog("3")
    }
    
    func setUpControl() {
        traceLog("4")
    }
}

요로코롬 호출하면됨..

해당 코드는 순서를 예측해서 traceLog에 찍어보았다. viewWillappear보다 viewDidLoad가 먼저 호출되기 때문에 이러한 순서가 나옴!

 

 

결과 값

 

이해가 힘들다면 뷰의 생명주기를 먼저 보길 권장합니당

 

iOS ) View Controller의 생명주기(Life-Cycle)

안녕하세요! 오늘은 View Controller생명 주기에 대해 알아보겠습니다.iOS를 시작하려고 하거나, 배우고 있는 분들이라면 반드시 알아야 해요.하나하나 제대로 알아봅시다 ㅎㅎ View Controller의 생명주

zeddios.tistory.com

 

 

 

728x90
반응형