from raywenderlich


# 언어 개선

Conditional Conformance

evolution: SE-0143

in standard library

  • Optional의 Wrapped가 Equatable을 상속받은 타입이라면 Optional도 Equatable이 되었다.
  • 이 말인 즉슨 [Int?] == [Int?] 비교가 가능해졌다는 이야기
  • [ [Int] ] == [ [Int] ]도 가능하다.
  • Array, Dictionary의 Value가 Equatable이면 Array, Dictionary도 Equatable이다.

in code


protocol Valuable {}

struct Property<Value> {
	var property: String
	var value: Value
}

struct TestValue: Valuable {}
struct Int: Valuable {}

extension Property: Equatable where Value: Equatable {
	static func == (lhs: Property<Value>, rhs: Property<Value>) -> Bool {
		return lhs.property == rhs.property && lhs.value == rhs.value
	}
}

let t1 = Property(property: "t1", value: 1)
let t2 = Property(property: "t2", value: 1)

print(t1 == t2)
  • 4.0까지는 상속 확장같은 제약조건이 있는 경우 where contaraint를 쓸 수가 없었지만, 4.1부터는 가능해졌다.

### in JSON parsing
  • Codable을 이용하는 경우에도 Optional, Array, Dictionary에서도 잘 작동합니다.

## JSON Encoding시에 카멜 케이스와 스네이크 케이스를 변환할 수 있다.
  • 인코딩시 키 문자열을 snake case로 변환하는 옵션이 추가되었다.
  • 디코딩시 키 문자열을 snake case에서 camel case로 변환하는 옵션이 추가되었다.

## Equatable, Hashable 프로토콜이 편해졌다.

evolution: SE-0185

  • boilerplate code 제거
  • 명시적으로 == 함수나 hashValue 프로퍼티를 선언하지 않아도 된다.

## Hashable Index Types

evolution: SE-0188

  • 배열의 index type이 Hashable이 되었다. 따라서 KeyPath로 사용이 가능하다.

## Protocol의 associated type에 재귀적 조건문선언이 가능해졌다.

evolution: SE-0157

protocol Hi {
	associatedtype Hello
	associatedtype World: Hi where World.Hello == Hello, World.World == World
}
  • 어따 쓰면 좋을지 아직 모르겠지만, 어쨌건 프로토콜의 where절을 사용할 때 지금껏 안되던 것이 되겠다는 희망이 생김

## Protocol에서 `weak`, `unowned` 지시자 제거

evolution: SE-0186

  • protocol에서 프로퍼티를 정의할 때, weak, unowned를 사용할 수 있었는데, 의미가 없는 선언자이기 때문에 사용하지 못하도록 제거

## Collection의 Index Distances

evolution: SE-0191

  • Collection에서 IndexDistance의 associatedtype을 제거하고, typealias를 통해 Int로 치환했다.
  • 따라서 Collection IndexDistance타입에 대해 더 이상 신경쓰지 않아도 된다.

Structure Initializers in Modules

evolution: SE-0189

  • struct를 별도의 모듈에서 확장하며, self의 프로퍼티에 값을 설정할 수 있었는데, 이것을 self.init(..) 혹은, self = ...로 명확하게 제한한다.
    • 위반시, swift 4에서는 경고, swift 5에서는 에러가 된다.

설정과 빌드

Build imports

evolution: SE-0075

  • 드디어 모듈 임포트 가능 여부를 체크 할 수 있다.
#if canImport(UIKit)
...
#endif

Target Environments

evolution: SE-0190

  • 타겟(기기)를 체크할 수 있다.
#if targetEnvironment(simulator)
...
#endif

기타 등등

Compacting Sequences

evolution: SE-0187

  • flatMap함수의 이름이 역할과의 관계가 모호했다.
  • 그래서 compactMap으로 바꿈

Unsafe Pointers

evolution: SE-0184

  • UnsafeMutableBufferPointer를 생성시 UnsafeMutablePointer를 사용하지 않아도 되도록 수정

New Playground Features

evolution: SE-0198

  • 모르겠다. 안써봐서.