티스토리 뷰

반응형

 

안녕하세요 :) Zedd입니다.

오늘은 SwiftLint의 오류 중 하나인

Compiler Protocol Init Violation: The initializers declared in compiler protocol ExpressibleByArrayLiteral shouldn't be called directly. 

이 warning에 대해서 알아보겠습니다.

 

# 어디서 발생?

let array = Array(arrayLiteral: 1, 2, 3)
let indexSet = IndexSet(arrayLiteral: 1, 2, 3) 
// Compiler Protocol Init Violation: The initializers declared in compiler protocol ExpressibleByArrayLiteral shouldn't be called directly. 

이런 친구들에게 warning이 발생하게 됩니다.

 

# 왜 발생? 

위 warning은 SwiftLint의 Compiler Protocol Init 룰에 의한것인데요, 

ExpressibleByArrayLiteral과 같은 프로토콜에 선언된 이니셜라이저를 직접 호출하면 안되는데,

프로토콜 이니셜라이저를 직접 호출해서 그렇습니다.

init(arrayLiteral:)의 애플 문서에 가보면, "Do not call this initializer directly"라고 나와있어요!

이러한 이니셜라이저는 컴파일러에서 사용된다고 해요. 

예를들어

let x = 23

x에 상수를 할당하는 것은 뒤에서(behind the scenes) literal initializer를 호출하게 되는 것이죠. 

 

ExpressibleBy*Literal은 대충

  • ExpressibleByArrayLiteral

  • ExpressibleByNilLiteral

  • ExpressibleByBooleanLiteral

  • ExpressibleByFloatLiteral

  • ExpressibleByIntegerLiteral

  • ExpressibleByUnicodeScalarLiteral

  • ExpressibleByExtendedGraphemeClusterLiteral

  • ExpressibleByStringLiteral

  • ExpressibleByStringInterpolation

  • ExpressibleByDictionaryLiteral

이런것들이 있습니다.

 

# 해소

Compiler Protocol Init Violation: The initializers declared in compiler protocol ExpressibleByArrayLiteral shouldn't be called directly. 

이 warning이 발생하지 않도록 하려면

let array = Array(arrayLiteral: 1, 2, 3)
let indexSet = IndexSet(arrayLiteral: 1, 2, 3) 
// Compiler Protocol Init Violation: The initializers declared in compiler protocol ExpressibleByArrayLiteral shouldn't be called directly. 

이렇게 쓰지 말아야겠죠..?!

let array = [1, 2, 3]
let indexSet = IndexSet([1, 2, 3]) 

이렇게 쓰도록 합시다!!! 

바꾸기 싫다면....swiftlint.yml의 disabled rules에 compiler_protocol_init를 추가해주면 됩니다!

disabled_rules:
  - compiler_protocol_init

 

반응형

'공부' 카테고리의 다른 글

LLVM이란  (2) 2020.12.03
Code Coverage  (2) 2020.10.07
Dart 톺아보기  (0) 2020.09.03
UITest ) UIButton의 isSelected가 항상 false로 나온다면  (0) 2020.09.02
Xcode ) Build Time Optimization. Diagnostic options 적용 후기  (2) 2020.08.18