Upgrade to Pro — share decks privately, control downloads, hide ads and more …

バックポートして学ぶ新APIの仕組み

Avatar for Yutaro Muta Yutaro Muta
September 11, 2022

 バックポートして学ぶ新APIの仕組み

Avatar for Yutaro Muta

Yutaro Muta

September 11, 2022
Tweet

More Decks by Yutaro Muta

Other Decks in Programming

Transcript

  1. yutailang0119/SBPAsyncImage • Backport of SwiftUI.AsyncImage to iOS 14, macOS 11,

    tvOS 14 and watchOS 7 and earlier. • SwiftUI.AsyncImageは2021年OSから ◦ iOS 15.0+ iPadOS 15.0+ macOS 12.0+ Mac Catalyst 15.0+ tvOS 15.0+ watchOS 8.0+
  2. SwiftUI.AsyncImage struct AsyncImage<Content> where Content : View AsyncImage(url: URL(string: “https://example.com/icon.png"))

    .frame(width: 200, height: 200) BackportAsyncImage struct BackportAsyncImage<Content> where Content : View BackportAsyncImage(url: URL(string: “https://example.com/icon.png")) .frame(width: 200, height: 200)
  3. なぜバックポートするのか • 例外もある • Swift Concurrency関連のAPIの一部は、  公式でバックポートされている ▪ Xcode 14.0で、URLSessionのバックポートが増えた

    iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 16.0+ Beta tvOS 13.0+ watchOS 6.0+ func data(from url: URL) async throws -> (Data, URLResponse)
  4. 新APIの調べ方 • Apple Developer Document ◦ API diff ◦ Technologies

    • Videos • Xcode ◦ Jump to Definition ⌃⌘
  5. 新APIの調べ方 • Apple Developer Document ◦ API diff ◦ Technologies

    • Videos • Xcode ◦ Jump to Definition ⌃⌘
  6. 新APIの調べ方 • Apple Developer Document ◦ API diff ◦ Technologies

    • Videos • Xcode ◦ Jump to Definition ⌃⌘ 根気強く見ていく
  7. サポートOSの範囲 • 2022年における基本はiOS 13+ ◦ Module Stabilityが達成されたSwift 5.1が同梱され たのがiOS 13

    ▪ Swift ConcurrencyのバックポートもiOS 13+ ◦ SwiftUIのAvailabilityがiOS 13+ • サポート範囲、動作の割り切りも必要 ◦ Appleも苦戦する領域
  8. 名前衝突を避ける工夫 • 伝統的なPrefix回避 ◦ e.g. ▪ IBP = iOS Backport

    ▪ SBP = Swift Backport • 冗長にバックポートとわかる名前を付ける ◦ e.g. BackportAsyncImage
  9. SE-0339 Module Aliasing For Disambiguation • SwiftPM 5.7+ ◦ Xcode

    14.0+ • moduleAliasesキーワードで、パッケージの 外からモジュール名を変更可能 • 利用側で指定が必要
  10. SE-0339 Module Aliasing For Disambiguation // swift-tools-version: 5.7 targets: [

    .executableTarget( name: "App", dependencies: [ .product(name: "Game", package: "swift-game", moduleAliases: ["Utils": "GameUtils"]), .product(name: "Utils", package: "swift-draw"), ]) ]
  11. 小さい機能を支える広い領域 • e.g. AsyncImage ◦ SwiftUI ▪ @StateObject, @ObservedObject, @Published,

    ViewBuilder ◦ UIKit ▪ UIImage ◦ AppKit ▪ NSImage ◦ Foundation ▪ URLSession
  12. OS毎の挙動差 • OSバージョン毎に利用できる手段の差異 ◦ iOS 14+ @StateObject ◦ iOS 13+

    @ObservedObject • プラットフォーム毎にフレームワークの差異 ◦ UIKit vs AppKit ▪ e.g. UIImage vs NSImage
  13. • typealiasでOriginに合わせ、意識せずに利用 させる ▪ ▪ ▪ @available ▪ モジュールを指名することで、使い分け可能 @available(iOS,

    deprecated: 15.0, renamed: "SwiftUI.AsyncImage") public typealias AsyncImage = BackportAsyncImage SBPAsyncImageの戦略
  14. References • https://github.com/yutailang0119/SBPAsyncImage • https://developer.apple.com/documentation/swiftui/asyncimage • https://developer.apple.com/documentation/foundation/urlsession/3919873-data • https://developer.apple.com/documentation •

    https://developer.apple.com/documentation/technologies?changes=latest_minor • https://yutailang0119.hatenablog.com/entry/2022/02/24/163000 • https://github.com/apple/swift-evolution/blob/main/proposals/0339-module-aliasing-f or-disambiguation.md • https://developer.apple.com/documentation/packagedescription/target/dependency/pr oduct(name:package:modulealiases:) • https://docs.swift.org/swift-book/ReferenceManual/Attributes.html