从Xcode iOS项目运行swift脚本作为构建阶段 [英] Run swift script from Xcode iOS project as build phase

查看:130
本文介绍了从Xcode iOS项目运行swift脚本作为构建阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的快速脚本:

Here is a simple swift script:

#!/usr/bin/env xcrun swift

import Foundation

let task = NSTask()
task.launchPath = "/bin/echo"
task.arguments = ["farg1", "arg2"]

let pipe = NSPipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)

print(output)

我在iOS项目中将此脚本添加为构建阶段(在'编译源阶段之前),但是XCode无法构建项目,错误'undefined NSTask()...'
当我添加OSX项目中的相同脚本,XCode构建项目没有任何错误。

I added this script as build phase(before 'compile sources phase) in iOS project but XCode failed to build project with error 'undefined NSTask()...' When I added same script in OSX project, XCode build project without any errors.

问题是为什么XCode在iOS框架内寻找NSTask(哪里不存在)而不是运行swift脚本作为平台脚本(如bash)?

Question is why XCode is looking for NSTask inside iOS framework(where doesn't exists) instead to run swift script as platform script(like bash)?

添加:swift脚本不包含在项目中要编译的其他文件。它只是在构建阶段,甚至在编译之前添加到其他源文件(目标c文件)来运行它。

Just to add: swift script isn't included into project with other files to compile. It's just added into build phase, even before compilation, of other source files(which are objective-c files) to run it.

任何想法如何运行自定义swift脚本来自iOS XCode项目,当脚本包含不属于iOS框架的类时?

Any ideas how to run custom swift script from iOS XCode project when script contains classes which aren't part of iOS framework?

推荐答案

为iOS构建隐式SDK时xcrun是iOS SDK,因此您可以使用命令行参数将其更改为Mac OS X SDK。将脚本中的第一行更改为:

When building for iOS the implicit SDK for xcrun is the iOS SDK, so you have the change it to the Mac OS X SDK with a command line parameter. Change the first line in your script to:

#!/usr/bin/env xcrun --sdk macosx swift

这篇关于从Xcode iOS项目运行swift脚本作为构建阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆