티스토리 뷰

Mobile/iOS

print, debugPrint

out of coding 2018. 12. 10. 11:42

콘솔에 debug를 찍을 방법이 두가지 존재합니다.


print와 debugPrint인데요.

아주 미묘하게 차이점을 가지고 있습니다.


1. String 출력시


print("test") // test

debugPrint("test") // "test"


원형을 그대로 찍어주게 됩니다. 이렇게 되는 이유는 아래와 같을수 있습니다.


2. 출력할 프로토콜이 서로 다르다.


print의 경우에는 CustomStringConvertible을 따릅니다.

debugPrint의 경우에는 CustomDebugStringConvertible을 따릅니다.


이 두가지의 차이를 보자면, description property와 debugDescription property를 사용하는지에 대한 차이점입니다.



결론적으로 본다면, 두가지는 거의 비슷하게 동작합니다.


그런데 한가지 다른점은 debug를 위한 용도로 생성이 되어서 객체 내부에서 구현되어 있을때 조금 더 자세한 내용을 표현하려고 노력합니다.


커다란 차이점은 없을것 같네요..


마지막으로 두 객체의 Custom시 입니다.


* print


1
2
3
4
5
6
7
struct CustomData: CustomStringConvertible {
    let data: Int
    
    var description: String {
        return "\(data)"
    }
}
cs


* debugPrint


1
2
3
4
5
6
7
struct CustomData: CustomDebugStringConvertible {
    let data: Int
    
    var debugDescription: String {
        return "\(data)"
    }
}
cs



'Mobile > iOS' 카테고리의 다른 글

swift. Codable의 변경사항. json snake to camel  (0) 2019.03.23
RxSwift. Hot, Cold Observe  (0) 2018.12.21
iOS. Associated References로 임의 객체 저장  (0) 2018.10.16
Swift. Codable Decoding (2)  (0) 2018.10.07
iOS. swift defer  (0) 2018.09.01
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함