如何在Xcode的Playground中导入RxSwift?

19

我正在尝试在 Xcode Playground 中导入 RxSwift,方法如下:

gem install cocoapods-playgrounds

之后

pod playgrounds RxSwift

但是现在还没有发生。怎么做呢?


您可以将游乐场添加到任何项目中。如果您知道如何安装RxSwift pod,则很好... - mfaani
7个回答

28
  1. 在您的计算机上克隆RxSwift存储库
  2. 打开Rx.xcworkspace
  3. 构建RxSwift-macOS方案
  4. Rx.xcworkspace树视图中打开Rx.playground并添加一个新页面
  5. 在新页面中导入RxSwift
  6. 选择View > Debug Area > Show Debug Area

步骤3和4

步骤5


我会尝试并让您对此提供反馈。 - Istiak Morsalin
@JasonBourne 我正在附上一些图片,以便逐步指导您。 - xandrefreire

8

这非常简单:

  1. 创建一个新的项目,Xcode菜单 > 文件 > 新建 > 项目… (Single View App)
  2. 关闭Xcode
  3. 在新项目中初始化pods(在终端中输入pod init)
  4. 编辑Podfile文件,添加RxSwift和RxCocoa
  5. 安装pods(在终端中输入pod install)
  6. 打开CocoaPods创建的RxSwiftProject.xcworkspace
  7. 使用默认方案构建并运行项目
  8. 添加一个新的playground,Xcode菜单 > 文件 > 新建 > Playground…
  9. 选择添加到:RxSwiftProject,将其添加到RxSwift工作空间
  10. 将其保存到工作区文件夹中

示例代码在此处


请为问题打上适当的技术标签,以便正在使用相关技术的人能够找到它。对于这个问题,请打上 swiftios 标签。 - Surya

3
@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),在那里找到代码。第一次使用可能需要点击 Product -> Build 来启动。

1

Xcode 9.1不再支持

$ pod playgrounds RxSwift,RxCocoa

会产生以下错误

Errno::ENOENT - 没有这样的文件或目录@dir_initialize-/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Playground with Platform Choice.xctemplate


1

您可以使用Arena创建一个带有第三方SPM库的游乐场:

arena https://github.com/finestructure/Gala
  resolving package dependencies
  libraries found: Gala
  created project in folder 'SPM-Playground'

那也适用于 RxSwift。


0

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

$ gem install cocoapods-playgrounds

$ pod playgrounds RxSwift,RxCocoa

然后它就会工作了。第二个命令将打开一个已经添加了pods的工作区。只需记得先构建目标,然后就可以在新的playground中玩耍了。


4
现在无法使用。pod playgrounds RxSwift,RxCocoa。为什么?Errno :: ENOENT - 没有这样的文件或目录@ dir_initialize - /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Playground with Platform Choice.xctemplate... - Pedro.Alonso

-1

太容易了!

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

  2. 在Xcode项目导航中,在playground文件下面,你会找到一个名为source的文件夹

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

  4. 然后就可以享受它了!

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接