如何从SWIFT中的字符串获取MD5哈希码并生成桥头 [英] How to get MD5 hash from string in SWIFT and make bridge-header
问题描述
#import< CommonCrypto / CommonCrypto.h> ;
首先编译器说'#'不好。然后我删除了,编译器说'<'不好。
我试图找出并找到建议添加名为CommonCrypto的文件夹并创建一个名为module.map的文件。我无法理解如何使用此扩展名创建文件。好的,我创建了swift文件并替换了它的扩展名。然后在那里写代码:
module CommonCrypto [system] {
header/ usr / include / CommonCrypto / CommonCrypto。 h
export *
}
并且再次不行
然后建议在项目设置$ {SRCROOT} / CommonCrypto中将新模块添加到Swift编译器下的导入路径 - 搜索路径中。
并且再次不好。
我不敢相信它很难做到这一点。我想我误解了一些步骤或什么。如果你知道一步一步的答案,请帮忙))
您需要添加一个桥接头并添加 #import< CommonCrypto / CommonCrypto.h>
语句。
添加桥接头的最简单方法是添加Objective-C文件到项目中,您将会ig希望添加一个桥接标题,答复yes。之后,您可以删除已添加的Objective-C文件。
示例代码:
func md5(#string:String) - > NSData {
var digest = NSMutableData(length:Int(CC_MD5_DIGEST_LENGTH))!
if data:NSData = string.dataUsingEncoding(NSUTF8StringEncoding){
CC_MD5(data.bytes,CC_LONG(data.length),
UnsafeMutablePointer< UInt8>(digest.mutableBytes))
返回摘要
//测试:
let digest = md5(string:这是测试字符串)
println (digest:\(digest))
输出:
摘要:8f833933 03a151ea 33bf6e3e bbc28594
这是一个更为Swift 2.0的版本返回一个 UInt8
数组:
func md5(string string:String ) - > [UInt8] {
var digest = [UInt8](count:Int(CC_MD5_DIGEST_LENGTH),repeatedValue:0)
if data = string.dataUsingEncoding(NSUTF8StringEncoding){
CC_MD5(data.bytes ,CC_LONG(data.length),& digest)
}
return digest
}
i dont even expect this problem, but it appears. I try to get md5 hash from string in swift. I search about that on SO and assume that i need to import library like that:
#import <CommonCrypto/CommonCrypto.h>
First of all compiler said that '#' is not okay. Then i removed and compiler said that '<' is not okay. I tried to figure out that and find recommendations to add folder named "CommonCrypto" and create a file named "module.map". I cant understand how to create file with this extension. Okay, i create swift file and replace its extension. Then write code there:
module CommonCrypto [system] {
header "/usr/include/CommonCrypto/CommonCrypto.h"
export *
}
and again its not okay Then in recommendations was adding the new module to Import Paths under Swift Compiler – Search Paths in your project settings ${SRCROOT}/CommonCrypto).
and its again not okay.
i cant belive that its so difficult to do that. i think i misunderstand some steps or something. if you know step by step answer please help))
You need to add a bridging header and add the #import <CommonCrypto/CommonCrypto.h>
statement to it.
The easiest way to add a bridging header is to add an Objective-C file to the project, you will be aked ig you want to add a bridging header, reply yes. After that you can delete the Objective-C file file that was added.
Example code:
func md5(#string: String) -> NSData {
var digest = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
if let data :NSData = string.dataUsingEncoding(NSUTF8StringEncoding) {
CC_MD5(data.bytes, CC_LONG(data.length),
UnsafeMutablePointer<UInt8>(digest.mutableBytes))
}
return digest
}
//Test:
let digest = md5(string:"Here is the test string")
println("digest: \(digest)")
Output:
digest: 8f833933 03a151ea 33bf6e3e bbc28594
Here is a more Swift 2.0 version returning an array of UInt8
:
func md5(string string: String) -> [UInt8] {
var digest = [UInt8](count: Int(CC_MD5_DIGEST_LENGTH), repeatedValue: 0)
if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
CC_MD5(data.bytes, CC_LONG(data.length), &digest)
}
return digest
}
这篇关于如何从SWIFT中的字符串获取MD5哈希码并生成桥头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!