无法调用copyBytes' &安培; "在Swift 3"中删除了C-style for语句。尝试将项目更新为swift 3

时间:2016-12-25 07:19:01

标签: ios swift cryptography swift3 hex

extension String {

        /// Create NSData from hexadecimal string representation
        ///
        /// This takes a hexadecimal representation and creates a NSData object. Note, if the string has any spaces, those are removed. Also if the string started with a '<' or ended with a '>', those are removed, too. This does no validation of the string to ensure it's a valid hexadecimal string
        ///
        /// The use of `strtoul` inspired by Martin R at http://stackoverflow.com/a/26284562/1271826
        ///
        /// - returns: NSData represented by this hexadecimal string. Returns nil if string contains characters outside the 0-9 and a-f range.

        func dataFromHexadecimalString() -> NSData? {
            let trimmedString = self.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<> ")).stringByReplacingOccurrencesOfString(" ", withString: "")

            // make sure the cleaned up string consists solely of hex digits, and that we have even number of them

            var error: NSError?
            let regex: NSRegularExpression?
            do {
                regex = try NSRegularExpression(pattern: "^[0-9a-f]*$", options: .CaseInsensitive)
            } catch let error1 as NSError {
                error = error1
                regex = nil
            }
            let found = regex?.firstMatchInString(trimmedString, options: [], range: NSMakeRange(0, trimmedString.characters.count))
            if found == nil || found?.range.location == NSNotFound || trimmedString.characters.count % 2 != 0 {
                return nil
            }

            // everything ok, so now let's build NSData

            let data = NSMutableData(capacity: trimmedString.characters.count / 2)

            for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = index.successor().successor() {
                let byteString = trimmedString.substringWithRange(Range<String.Index>(start: index, end: index.successor().successor()))
                let num = UInt8(byteString.withCString { strtoul($0, nil, 16) })
                data?.appendBytes([num] as [UInt8], length: 1)
            }

            return data
        }
    }

尝试将上面代码snippit中的for循环转换为swift 3,目前用swift 2.3编写并遇到麻烦。错误是:&#34;在Swift 3&#34;

中删除了C-style for语句

以下for循环是我按下&#34;转换为swift 3&#34; xcode上的按钮。

   for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = <#T##Collection corresponding to your index##Collection#>.index(after: <#T##Collection corresponding to `index`##Collection#>.index(after: index)) {
            let byteString = trimmedString.substring(with: (index ..< <#T##Collection corresponding to your index##Collection#>.index(after: <#T##Collection corresponding to `index`##Collection#>.index(after: index))))
            let num = UInt8(byteString.withCString { strtoul($0, nil, 16) })
            data?.append([num] as [UInt8], length: 1)
        }
    extension NSData {

        /// Create hexadecimal string representation of NSData object.
        ///
        /// - returns: String representation of this NSData object.

        func hexadecimalString() -> String {
            let string = NSMutableString(capacity: length * 2)
            var byte: UInt8 = 0

            for i in 0 ..< length {
                getBytes(&byte, range: NSMakeRange(i, 1))
                string.appendFormat("%02x", byte)
            }

            return string as String
        }
    }

上面的代码片段中的for循环也没有在swift 3中工作。如何为swift 3重写这个?这个错误是:&#34;无法调用&#39; copyBytes&#39;使用类型&#39;的参数列表(to:inout UInt8,from:NSRange)&#39;&#34;

任何帮助表示赞赏。我使用这些函数为我正在使用的第三方服务构建一个特殊的URL,但是很难将这个复杂的语法更新为swift 3.

错误here

的屏幕截图

1 个答案:

答案 0 :(得分:2)

有很多线程解释如何转换C风格的for循环,或者如何在Swift 3中使用Data。(在Swift 3中,你最好使用Data而不是NSData。)您只需找到并合并它们。

extension String {

    func dataFromHexadecimalString() -> Data? {
        let trimmedString = self.trimmingCharacters(in: CharacterSet(charactersIn: "<> ")).replacingOccurrences(of: " ", with: "")

        //`NSRegularExpression(pattern:options:)` will not throw error for a valid pattern & options.
        //And you need to use `utf16.count` when working with `NSRegularExpression`.
        let regex = try! NSRegularExpression(pattern: "^[0-9a-f]*$", options: .caseInsensitive)
        let found = regex.firstMatch(in: trimmedString, range: NSMakeRange(0, trimmedString.utf16.count))
        if found == nil || found!.range.location == NSNotFound || trimmedString.characters.count % 2 != 0 {
            return nil
        }

        //In Swift 3, working with `Data` is easier than `NSData` in most cases.
        var data = Data(capacity: trimmedString.characters.count / 2)

        //Generally, `for INIT; COND; UPDATE {...}` can be re-written with `INIT; while COND {...; UPDATE}`
        var index = trimmedString.startIndex
        while index < trimmedString.endIndex {
            let nextIndex = trimmedString.characters.index(index, offsetBy: 2)
            let byteString = trimmedString.substring(with: index..<nextIndex)
            let num = UInt8(byteString, radix: 16)!
            data.append(num)

            index = nextIndex
        }

        return data
    }
}

extension Data {

    func hexadecimalString() -> String {
        var string = ""
        string.reserveCapacity(count * 2)

        //You have no need to use `getBytes(_:range:)` when you use each byte of Data one by one.
        for byte in self {
            string.append(String(format: "%02X", byte))
        }

        return string
    }
}

某些部分可以用更快的方式重写,但我保留了一些代码的基本结构,以便比较两个代码。