WWDC18 の what’s new in swift のまとめ



swift4.2への移行は、xcodeから Convert > To Current Swift Syntax...

SE-0194 Derived Collection of Enum Cases


enum Gait: CaseIterable {
    case walk
    case trot
    case canter
    case gallop

for gait in Gait.allCases {


SE-0143 Conditional Conformances


SE-0185 Synthesizing Equatable and Hashable Conformance

EquatableHashable を書いておけば、明示的に ==hashValue を実装しなくてもokになった

// before
struct Restaurant {
    let name: String
    let hasTableService: Bool
    let kidFriendly: Bool

extension Restaurant: Equatable {
    static func ==(a: Restaurant, b: Restaurant) -> Bool {
        return a.name == b.name &&
            a.hasTableService == b.hasTableService &&
                a.kidFriendly == b.kidFriendly

// after
struct Restaurant: Equatable {
    let name: String
    let hasTableService: Bool
    let kidFriendly: Bool

SE-0206 Hashable Enhancements


public protocol Hashable : Equatable {

    /// The hash value.
    /// Hash values are not guaranteed to be equal across different executions of
    /// your program. Do not save hash values to use during a future execution.
    public var hashValue: Int { get }

    /// Hashes the essential components of this value by feeding them into the
    /// given hasher.
    /// Implement this method to conform to the `Hashable` protocol. The
    /// components used for hashing must be the same as the components compared
    /// in your type's `==` operator implementation. Call `hasher.combine(_:)`
    /// with each of these components.
    /// - Important: Never call `finalize()` on `hasher`. Doing so may become a
    ///   compile-time error in the future.
    /// - Parameter hasher: The hasher to use when combining the components
    ///   of this instance.
    public func hash(into hasher: inout Hasher)

SE-0202 Random Unification

  • 簡単に乱数の生成ができるようになった
  • randomElement() でコレクションからランダムで要素を取り出せるようになった
  • shuffled() でコレクションをシャッフルできるようになった
// 乱数の生成
let randomIntFrom0To10 = Int.random(in: 0 ..< 10)
let randomFloat = Float.random(in: 0 ..< 1)

// コレクションからランダムで要素を取り出す
let greetings = ["hey", "hi", "hello", "hola"]
print(greetings.randomElement()!) // "hello"

// コレクションの要素をシャッフル
let randomlyOrderedGreetings = greetings.shuffled()
print(randomlyOrderedGreetings) // ["hi", "hola", "hello", "hey"]

SE-0190 Target Environment Platform Condition


// Test for a simulator destination
#if (arch(i386) || arch(x86_64)) && (!os(macOS))

// More restrictive test for iOS simulator
// Adjust the os test for watchOS, tvOS
#if (arch(i386) || arch(x86_64)) && os(iOS)
    // iOS simulator code

このように書いていたが、#if canImport(UIKit)#if hasTargetEnvironment(simulator)と書けるようになった



メールアドレスが公開されることはありません。 * が付いている欄は必須項目です