[swift2] Xcode7 beta6 Swift 언어 변경사항

Swift 2015. 8. 27. 01:48


Swift Language Enhancements and Changes


1. try? 키워드가 추가되었습니다.

  • try? 구문은 에러를 던질수도 있는 연산을 수행합니다.
  • 연산이 성공하면 결과는 optional로 랩핑되어 반환되고, 에러가 throw 되면 결과는 nil를 반환하고 error는 버려집니다.
  • try?는 if let과 guard 문과 함께 사용될 때 특히 유용합니다.
    func produceGizmoUsingTechnology() throws -> Gizmo { … }
    func produceGizmoUsingMagic() throws -> Gizmo { … }

    if let result = try? produceGizmoUsingTechnology() { return result }
    if let result = try? produceGizmoUsingMagic() { return result }
    print("warning: failed to produce a Gizmo in any way")
    return nil
  • try?에 의해 평가되는 표현식의 결과타입에 항상 optional이 추가된다는 것을 주목하세요.
  • throwing 함수의 반환타입이 Int?이면, try?와 함께 함수를 호출한 결과는 Int?? 또는 'Optional<Optional>' 이 됩니다.


2. Xcode는 . 문법을 사용할 때, 문맥을 인식한 enum 엘리먼트와 option sets의 코드완성 기능을 제공합니다.


3. 프로토콜 extension Static 계산속성을 정의할 수 있습니다.


4. 함수 또는 생성자 파라미터 리스트 어떤 위치에도 가변인자가 올 수 있습니다.

func doSomethingToValues(values: Int..., options: MyOptions = [], fn: (Int) ->
Void) { 
    // function body 
}


5. Objective-C와 호환되지 않는 타입을 포함하는 컬렉션은 더이상 Objective-C 호환타입으로 간주되지 않습니다.

  • 이전에는 Array 구문은 @objc가 마킹된 프로퍼티로 허용되었지만, 그러한 케이스는 더이상 허용되지 않습니다.


6. C typdefs으로 선언된 block은 이제 Swift 클로져 typealias로 임포트됩니다.

  • BOOL 타입 파라미터가 사용된 typedefs 블럭은 Bool 타입 파라미터를 가진 클로져로 임포트됩니다.(이전 베타5에서는 ObjCBool 파리미터로 되었음.)
  • 이것은 블럭 파라미터의 행동과 임포트된 Objective-C 메서드를 매칭시킵니다.


7. 타입체커가 생성한 에러메시지는 구체적이고 유용하게 개선되고 있습니다.

  • 진단시에, type alias를 사용했다면 별칭(aka)을 출력합니다.


Swift Standard Library Enhancements and Changes


1. print()와 debugPrint()가 개선되었습니다.

  • 가변인자 지원하여, 한번의 호출로 여러개의 아이템을 출력할수 있습니다.
  • separator: String = " " 가 추가되어, 어떻게 아이템이 구분되는지 제어할 수 있습니다.
  • appendNewline:bool = true 는 terminator:String = "\n"로 교체되었습니다.
  • output stream을 인자로 받는 print 함수에는 stream 인자에 toStream 레이블을 추가했습니다.


2. RangeReplaceableCollectionType.extend() 메소드가 appendContentsOf()로 이름이 변경되었습니다.

  • splice() 메서드는 insertContentsOf()로 이름이 변경되었습니다.


3. 클로져와 @autoclosure 인자를 갖는 대부분의 표준라이브러리 API은 이제 rethrows를 사용합니다.

  • map과 filter 같은 메서드의 클로져 파라미터들이 에러를 throw할 수 있게 허용합니다.
  • 에러를 발생시킬 수 있는 표현식과 &&, ||, ?? 같은 short-circuiting 연산자를 함께 사용할수 있습니다.


4. 모든 CollectionType은 이제 sliceable 합니다.

  • 다음 sequence spllitting/slicing 함수들은 SequenceType 프로토콜 요구사항에 맞게 삭제되거나 프로토콜 extension 디폴트 구현으로 대체되었습니다.
/// Returns the first `maxLength` elements of `self`,
/// or all the elements if `self` has fewer than `maxLength` elements.
prefix(maxLength: Int) -> SubSequence

/// Returns the last `maxLength` elements of `self`,
/// or all the elements if `self` has fewer than `maxLength` elements.
suffix(maxLength: Int) -> SubSequence

/// Returns all but the first `n` elements of `self`.
dropFirst(n: Int) -> SubSequence

/// Returns all but the last `n` elements of `self`.
dropLast(n: Int) -> SubSequence

/// Returns the maximal `SubSequence`s of `self`, in order, that
/// don't contain elements satisfying the predicate `isSeparator`.
split(maxSplits maxSplits: Int, allowEmptySlices: Bool, @noescape isSeparator:
(Generator.Element) -> Bool) -> [SubSequence]
  • split를 위해 다음 convenience extension을 제공합니다.
split(separator: Generator.Element, maxSplit: Int, allowEmptySlices: Bool) ->
[SubSequence]  
  • 또한 새로운 프로토콜 요구사항과 컬렉션 타입의 디폴트 구현이 사용가능합니다.
/// Returns `self[startIndex..<end]`
prefixUpTo(end: Index) -> SubSequence

/// Returns `self[start..<endIndex]`
suffixFrom(start: Index) -> SubSequence

/// Returns `prefixUpTo(position.successor())`
prefixThrough(position: Index) -> SubSequence