我发现了一个在SWIFT框架中成功使用CommonCrypto的GitHub项目:SHA256-Swift.另外,这篇关于the same problem with sqlite3的文章也很有用.
基于上述情况,具体步骤如下:
1)在项目目录内创建CommonCrypto
目录.在中,创建一个module.map
文件.模块映射将允许我们将CommonCrypto库用作SWIFT中的模块.它的内容是:
module CommonCrypto [system] {
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/usr/include/CommonCrypto/CommonCrypto.h"
link "CommonCrypto"
export *
}
2)在生成设置中,在Swift Compiler - Search Paths内,将CommonCrypto
目录添加到Import Paths(SWIFT_INCLUDE_PATHS
).
3) 最后,在Swift文件中导入CommonCrypto和其他模块一样.例如:
import CommonCrypto
extension String {
func hnk_MD5String() -> String {
if let data = self.dataUsingEncoding(NSUTF8StringEncoding)
{
let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
let resultBytes = UnsafeMutablePointer<CUnsignedChar>(result.mutableBytes)
CC_MD5(data.bytes, CC_LONG(data.length), resultBytes)
let resultEnumerator = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, length: result.length)
let MD5 = NSMutableString()
for c in resultEnumerator {
MD5.appendFormat("%02x", c)
}
return MD5
}
return ""
}
}
限制
在另一个项目中使用自定义框架在编译时失败,出现错误missing required module 'CommonCrypto'
.这是因为CommonCrypto模块似乎不包含在自定义框架中.解决方法是在使用框架的项目中重复步骤2(设置Import Paths
).
模块映射不是独立于平台的(它当前指向一个特定的平台,即iOS 8模拟器).我不知道如何使标题路径相对于当前平台.
iOS8<;更新=我们应该删除第link "CommonCrypto"行,才能成功编译.
UPDATE / EDIT个
我一直收到以下生成错误:
ld:找不到体系 struct x86_64的-lCommonCrypto的库
clang:错误:链接器命令失败,退出代码为%1(使用-v查看调用)
除非我从我创建的module.map
文件中删除了第link "CommonCrypto"
行.一旦我移除这条线,它就可以正常工作了.