在Swift中获取Cocoa Touch Framework项目版本字符串 [英] Get Cocoa Touch Framework project version string in Swift

查看:116
本文介绍了在Swift中获取Cocoa Touch Framework项目版本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cocoa Touch Frameworks支持版本控制,可在版本控制部分的构建设置中找到。



要在运行时访问此值,我们可以使用自动生成的 FrameworkVersionNumber FrameworkVersionString [] 变量对于我们来说,作为构建过程的一部分。



使用Swift项目时,可以在Objective-C兼容性标题的顶部自动生成这些项目:

  //! Framework的项目版本号。 
FOUNDATION_EXPORT double FrameworkVersionNumber;

//! Framework的项目版本字符串。
FOUNDATION_EXPORT const unsigned char FrameworkVersionString [];

但是,可以从Swift访问 FrameworkVersionNumber FrameworkVersionString [] 不是。实际上看一下框架模块的内容,我可以看到只有第一个变量暴露给Swift:

  // ! Framework的项目版本号。 
var FrameworkVersionNumber:Double

这个问题是因为 FrameworkVersionNumber Double ,任何版本号如3.2.1只需更改为3.200000 ...



有谁知道这是否是我的项目设置中的缺陷,Xcode中的一个错误,或者是否有一种方法可以将Swift中的框架版本作为 String 或数组,以便我可以提供比 major.minor 更精细的版本控制?

解决方案

我实际上找到了这个问题的潜在解决方法,它不是那么干净但它确实有效:



默认情况下,当Xcode创建一个框架时,它会设置版本 1.0 构建 $(CURRENT_PROJECT_VERSION)哪个好,因为此值实际上是从构建设置>版本控制中的当前项目版本字段中复制的/ p>

那么你在运行时获得这个值的方法如下:

  let bundle = NSBundle(标识符:com.yourframework.Framework)! //从你的框架中获取对bundle的引用(不是app本身的包!)
let build = bundle.infoDictionary![kCFBundleVersionKey] as!字符串//从框架的包中获取构建字符串

这确实有效,但感觉很迂回对于过去(我相信)可以从Objective-C中的变量轻松访问的东西。


Cocoa Touch Frameworks provide support for versioning, which can be found in Build Settings under the Versioning section.

To access this value at runtime, we can use the FrameworkVersionNumber and FrameworkVersionString[] variables which are auto-generated for us as part of the build process.

When working with a Swift project, these can be found auto-generated at the top of the Objective-C compatibility header:

//! Project version number for Framework.
FOUNDATION_EXPORT double FrameworkVersionNumber;

//! Project version string for Framework.
FOUNDATION_EXPORT const unsigned char FrameworkVersionString[];

However, whilst FrameworkVersionNumber is accessible from Swift, FrameworkVersionString[] is not. In fact looking at the contents of the framework module, I can see that only the first variable is exposed to Swift:

//! Project version number for Framework.
var FrameworkVersionNumber: Double

The problem with this is that since FrameworkVersionNumber is a Double, any version numbers like 3.2.1 simply get changed to 3.200000...

Does anyone know whether this is a flaw in my project setup, a bug in Xcode, or whether there is a way of getting the framework version in Swift as a String or array, so that I can provide more granular versioning than major.minor?

解决方案

I have actually found a potential workaround for this issue, it's not so clean but it does work:

By default, when Xcode creates a framework it sets the Version to 1.0 and the Build to $(CURRENT_PROJECT_VERSION) which is great, because this value is actually being copied from the Current Project Version field in Build Settings > Versioning.

So what you can do to get this value at runtime is as follows:

let bundle = NSBundle(identifier: "com.yourframework.Framework")! // Get a reference to the bundle from your framework (not the bundle of the app itself!)
let build = bundle.infoDictionary![kCFBundleVersionKey] as! String // Get the build from the framework's bundle as a String

This does work but it feels quite circuitous for something that used to (I believe) be readily accessible from a variable in Objective-C.

这篇关于在Swift中获取Cocoa Touch Framework项目版本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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