Swift 2.2

3월 21일에 Swift 2.2가 정식 버전이 되었다. Swift 블로그에 어떤 내용이 변경되었는지 나왔는데, 하나씩 살펴보았다.

SE-0001: Allow (most) keywords as argument labels

함수 인자의 외부 이름 라벨(argument label)에 in 같은 키워드를 사용하기 위해서는 `in` 형태로 사용해야 했다.

func test1(name: String, `in` inString: String) {
    print("\(name) in \(inString)")
}

이번 버전부터는 inout, var, let을 제외한 키워드 대부분을 사용할 수 있게 되었다.

func test1(name: String, in inString: String) {
    print("\(name) in \(inString)")
}

SE-0015: Tuple comparison operators

튜플에 대한 <, > 같은 비교 연산이 추가되었다.

(1, "zebra") < (2, "apple")   // true because 1 is less than 2
(3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog”

SE-0014: Constraining AnySequence.init

이건 아직 자세히 모르겠다. 일단 넘어간다. ㅋㅋㅋ

SE-0011: Replace typealias keyword with associatedtype for associated type declarations

typealias 키워드가 다음 2가지 형태로 사용되어왔다.

  • Type Aliases - 이미 존재하는 type에 대한 별칭
  • Associated Type - protocol의 한 부분으로 앞으로 지정할 type에 대한 별칭

typealias는 Type Alias로만 사용되게 수정되고, associatedtype 이라는 새로운 키워드를 추가해 Associated Type으로 사용하게 되었다.

기존에는 이렇게 사용되었다.

protocol Container {
    typealias ItemType
    mutating func append(item: ItemType)
}

struct IntStack: Container {

    /* 중간 생략 */

    typealias ItemType = Int
    mutating func append(item: Int) {
        self.push(item)
    }
}

Swift 2.2에서는 다음과 같이 사용된다.

protocol Container {
    associatedtype ItemType
    mutating func append(item: ItemType)
}

struct IntStack: Container {

    /* 중간 생략 */

    typealias ItemType = Int
    mutating func append(item: Int) {
        self.push(item)
    }
}

SE-0021: Naming Functions with Argument Labels

함수를 구분할 때, 인자 이름까지 포함해서 구분할 수 있게 되었다.

The Swift Programming Language for Swift 2.2에서 예제 코드를 가지고 왔다.

class SomeClass {
    func someMethod(x: Int, y: Int) {}
    func someMethod(x: Int, z: Int) {}
    func overloadedMethod(x: Int, y: Int) {}
    func overloadedMethod(x: Int, y: Bool) {}
}
let instance = SomeClass()
 
let a = instance.someMethod              // Ambiguous
let b = instance.someMethod(_:y:)        // Unambiguous
 
let d = instance.overloadedMethod        // Ambiguous
let d = instance.overloadedMethod(_:y:)  // Still ambiguous
let d: (Int, Bool) -> Void  = instance.overloadedMethod(_:y:)  // Unambiguous

SE-0022: Referencing the Objective-C selector of a method

#selector 표현이 추가되었다.

Objective-C 런타임에서 참조 가능한 method를 가리킬 때, 사용한다고 한다.

class SomeClass: NSObject {
    @objc(doSomethingWithInt:)
    func doSomething(x: Int) { }
}
let x = SomeClass()
let selector = #selector(x.doSomething(_:))

Objective-C와 Swift를 같이 사용하지 않아서 유용성은 잘 모르겠다.

SE-0020: Swift Language Version Build Configuration

Swift 언어마다 차이가 커지니, 언어 버전에 따라 분기 처리하는 방법이 추가되었다.

#if swift(>=2.2)
  print("Active!")
#else
  this! code! will! not! parse! or! produce! diagnostics!
#endif

블로그에 안 나와 있는 변경 사항

The Swift Programming Language for Swift 2.2 문서의 Document History를 보면 더 많은 내용이 있다. 그중에 눈에 띄는 것들을 뽑았다.

++, --, C style for loop들이 deprecated 되었다.

컴파일 에러는 아니고, 경고 정도로 처리된다. Swift 3.0 준비 작업 중에 하나라고 한다.

참고

Comments