简体   繁体   English

如何为不同版本的Xcode编写条件

[英]How to Write a Conditional for Different Versions of Xcode

I am working on an app from two different computers. 我正在使用两台不同的计算机上的应用程序。 One is my home machine, which is an older MacBook Pro, but has the latest OS and is running Xcode 7.3. 一个是我的家用机器,它是较旧的MacBook Pro,但具有最新的操作系统并运行Xcode 7.3。 The second machine I use is my work machine, which is brand new and lightning fast, but is restricted to Yosemite and Xcode 7.2.1. 我使用的第二台机器是我的工作机器,它是全新的,闪电般快,但仅限于Yosemite和Xcode 7.2.1。

I recently encountered a build error on the machine running Xcode 7.2.1, but the app builds and runs without error on the machine running the newer Xcode. 我最近在运行Xcode 7.2.1的机器上遇到了构建错误,但是应用程序在运行新Xcode的机器上构建并运行时没有错误。 I cannot upgrade the work machine due to implacable IT policy, and I really (really) do not wish to downgrade my home machine to Xcode 7.2.1. 由于IT策略不可靠,我无法升级工作机器,我真的(真的)不希望将我的家用机器降级到Xcode 7.2.1。

So what I would like to do is write a conditional similar to the following pseudocode: 所以我想做的是编写一个类似于以下伪代码的条件:

if Xcode.version == 7.3
   // Run this version of the statement
   refreshControl.addTarget(self, action: #selector(ReadingTVC.pullToRefreshTableView), forControlEvents: UIControlEvents.ValueChanged)
if Xcode.version == 7.2.1
   // Run this different version of the statement
   // I still need to figure out how to rewrite the statement for 7.2.1

Is this possible? 这可能吗? I found the following in Apple documentation, but there is no option for Xcode versions. 我在Apple文档中找到了以下内容,但Xcode版本没有选项。 only swift(), os() or arch(): 只有swift(),os()或arch():

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/doc/uid/TP40014097-CH33-ID539 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/doc/uid/TP40014097-CH33-ID539

Thanks in advance! 提前致谢!

I can't test it currently but I think that 7.3 and 7.2.1 also have different Swift versions so you could use swift(). 我目前无法测试它,但我认为7.3和7.2.1也有不同的Swift版本,所以你可以使用swift()。 Apart from that I assume that the error is due to a change in Swift (the #selector syntax?) so this check would fit better anyway. 除此之外我假设错误是由于Swift的变化(#selector语法?)所以这个检查无论如何都会更好。

PS: instead of #selector(...) the older version just wants "pullToRefreshTableView" as selector. PS:而不是#selector(...)旧版本只需要“pullToRefreshTableView”作为选择器。

You should just use swift() to check the version of swift because Xcode 7.3 comes with Swift 2.2 and Xcode 7.2.1 comes with Swift 2.1.x. 您应该使用swift()来检查swift的版本,因为Xcode 7.3附带Swift 2.2而Xcode 7.2.1附带Swift 2.1.x.

For your code, since swift() is introduced in swift 2.2, you need to change use code that works in both versions, which is like below (Assuming ReadingTVC is self ): 对于你的代码,因为在swift 2.2中引入了swift() ,你需要更改在两个版本中都有效的使用代码,如下所示(假设ReadingTVCself ):

// For both swift 2.1.x and swift 2.2. In swift 2.2, this will pop a 
// warning and ask you to fix. But you can wait until you work computer
// updated Xcode version to 7.3. You will be force to change when 
// upgrade to swift 3.0 since the old string based api will be removed.
// Currently it is just marked as deprecated.
refreshControl.addTarget(self, action: "pullToRefreshTableView", forControlEvents: UIControlEvents.ValueChanged)

little tip for example for annoying String history... 例如烦人的字符串历史的小提示......

        #if swift(>=4.0)
            let len = text.count
        #else
            let len = text.characters.count
        #endif

works in Xcode 8.0 and higher (tested also in Xcode 9 beta) 适用于Xcode 8.0及更高版本(也在Xcode 9 beta中测试过)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM