如何在 xcode 的 Playground 中导入 RxSwift?

How to import RxSwift in Playground of xcode?

我正在尝试通过以下方式在 xcode playground 中导入 rxswift:

gem install cocoapods-playgrounds

之后

pod playgrounds RxSwift

但这并没有发生。怎么做?

  1. 您应该在计算机上克隆 RxSwift repository
  2. 打开Rx.xcworkspace
  3. 构建RxSwift-macOS方案
  4. Rx.xcworkspace 树视图中打开 Rx.playground 并添加一个新页面
  5. import RxSwift在新页面中。
  6. 选择View > Debug Area > Show Debug Area

@Jason,你需要运行以下命令:

$ gem install cocoapods-playgrounds

$ pod playgrounds RxSwift,RxCocoa

它会起作用的。第二个命令打开一个已添加 pods 的工作区。只记得先建立一个目标,然后你就可以玩你的新游乐场了。

不再适用于 Xcode 9.1

$ pod playgrounds RxSwift,RxCocoa

给予

Errno::ENOENT - No such file or directory @ dir_initialize - /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Playground with Platform Choice.xctemplate

太简单了!

  1. 在你的项目中创建playground(当然你需要添加RxSwift的依赖)

  2. 在Xcode项目导航中,playground文件下面,你会发现一个名为source

  3. 的文件夹
  4. 在此文件夹中创建此 swift 文件: https://github.com/ReactiveX/RxSwift/blob/master/Rx.playground/Sources/SupportCode.swift

  5. 那就尽情享受吧!

    // example
    
    import RxSwift
    
    playgroundShouldContinueIndefinitely()
    
    example("of") {
    
    let disposeBag = DisposeBag()
    
    Observable.of("", "", "", "")
        .subscribe(onNext: { element in
            print(element)
        })
    }
    

超级简单:

  1. 创建一个新项目,Xcode菜单>文件>新建>项目...(单 查看应用程序)
    1. 关闭Xcode
    2. 在新项目中初始化pods(终端中的pod init)
    3. 编辑 Podfile,添加 RxSwift、RxCocoa
    4. 安装pods(在终端安装pod)
    5. 打开由 CocoaPods
    6. 创建的 RxSwiftProject.xcworkspace
    7. 使用默认方案构建和运行项目
    8. 添加一个新的游乐场,Xcode 菜单 > 文件 > 新建 > 游乐场…
    9. Select 添加到:RxSwiftProject ,将其添加到 RxSwift 工作区
    10. 将其保存在工作区文件夹中

example code here

您可以使用 Arena:

使用第 3 方 SPM 库创建一个 playground
arena https://github.com/finestructure/Gala
  resolving package dependencies
  libraries found: Gala
✅  created project in folder 'SPM-Playground'

这应该也适用于 RxSwift。

正如@sas 所暗示的,您可以使用Arena

更具体地说:


安装 Arena 的一行:

brew install finestructure/tap/arena

一行创建支持 RxSwift 的 Playground:

arena https://github.com/ReactiveX/RxSwift

结果:

➡️  Package: https://github.com/ReactiveX/RxSwift @ from(5.1.1)
 Resolving package dependencies ...
 Libraries found: RxSwift, RxCocoa, RxRelay, RxBlocking, RxTest
 Building package dependencies ...
✅ Created project in folder 'Arena-Playground'

完成!打开 Arena-Playground 并找到您的 Playground(可能称为 MyPlayground),并在那里编写代码。第一次可能需要单击“产品”->“构建”。