无Xcode项目时向Swift Playground添加CocoaPods遇错求解决方案
问题描述
我尝试给Swift Playground集成CocoaPods时,先执行pod init就遇到了报错:
[!] No Xcode project found, please specify one
于是我去查了Stack Overflow的方案,试了下面两步:
gem install cocoapods-playgroundspod playgrounds Alamofire
结果又碰到了新的错误:
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 /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/generate.rb:43:in `open' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/generate.rb:43:in `foreach' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/generate.rb:43:in `dir_for_platform' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/generate.rb:10:in `initialize' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/workspace.rb:26:in `new' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/workspace.rb:26:in `block in generate' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/workspace.rb:23:in `chdir' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/workspace.rb:23:in `generate' /Library/Ruby/Gems/2.3.0/gems/cocoapods-playgrounds-1.2.2/lib/cocoapods-playgrounds/command/playgrounds.rb:46:in `run' /Library/Ruby/Gems/2.3.0/gems/claide-1.0.2/lib/claide/command.rb:334:in `run' /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.5.0/lib/cocoapods/command.rb:52:in `run' /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.5.0/bin/pod:55:in `<top (required)>' /usr/local/bin/pod:22:in `load' /usr/local/bin/pod:22:in `<main>'
有没有办法解决这个问题?
解决方案
我之前也碰到过一模一样的问题,这个错误本质是cocoapods-playgrounds插件依赖的Xcode模板路径在新版本Xcode里被改动了,或者工具版本和Xcode不兼容。给你几个亲测有效的解决办法:
先试试更新工具,最简单高效
首先把Xcode更到最新版本(App Store里检查更新就行),然后更新CocoaPods和它的playgrounds插件:sudo gem update cocoapods sudo gem update cocoapods-playgrounds更新完再跑
pod playgrounds Alamofire,新版本的插件一般会修复这种路径适配问题,大概率能直接解决。手动创建带Pod的Playground,稳得一批
如果更新工具还是不行,咱们就绕开插件,手动来:- 打开Xcode,新建一个普通的iOS App项目(名字随便取,比如
PlaygroundPodDemo) - 到项目根目录执行
pod init,然后编辑Podfile,把你要的库加进去,比如:target 'PlaygroundPodDemo' do use_frameworks! pod 'Alamofire' end - 执行
pod install,然后关闭Xcode,打开生成的.xcworkspace文件 - 在这个工作区里,点菜单栏
File > New > Playground,选好平台(比如iOS),把Playground添加到项目里 - 现在就能在Playground里
import Alamofire用了,注意下Playground的目标版本要和项目一致哦。
- 打开Xcode,新建一个普通的iOS App项目(名字随便取,比如
手动修复模板路径,适合喜欢折腾的
错误提示找不到那个模板文件,咱们可以给它整个软链接补上:- 打开Finder,前往路径
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/ - 看看里面有没有
Playground.xctemplate这个文件,有的话就执行下面的命令创建软链接:sudo ln -s /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Playground.xctemplate /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Playground with Platform Choice.xctemplate
做完这个再跑
pod playgrounds Alamofire应该就能正常工作了。- 打开Finder,前往路径
内容的提问来源于stack exchange,提问作者user2924482
相关产品推荐
相关产品推荐

