2016-10-04 60 views
-2

我收到錯誤在我的代碼斯威夫特3遷移錯誤

func dataFromHexadecimalString(_ key:String) -> NSString? { 

    let trimmedString = key.trimmingCharacters(in: CharacterSet(charactersIn: "<> ")).replacingOccurrences(of: " ", with: "") 


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


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

    //I am Getting error here i.e C-style for statement has been removed in Swift 3 
    for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = Collection.index(after: Collection.index(after: index)) { 
     let byteString = trimmedString.substring(with: (index ..< Collection.index(after: Collection.index(after: index)))) 
     let num = UInt8(byteString.withCString { strtoul($0, nil, 16) }) 
     data?.append([num] as [UInt8], length: 1) 
    } 

    let enCodedUTF8String = NSString(data: data! as Data, encoding: String.Encoding.utf8.rawValue) 

    return enCodedUTF8String 

} 

得到錯誤即C-style for statement has been removed 見上從我可以告訴你的代碼,代碼

+3

這是正確的,在Swift 3中C樣式for循環已被刪除。我會爲您的代碼提供一個修復程序,但我不知道它在做什麼。 'Collection.index(after:Collection.index(after:index)))'???你實際上在你的代碼中做什麼?我有一種感覺,有一種更簡單更優雅的方式來做你想做的事情。 – Fogmeister

+1

似乎傳遞的'key'參數是一個'Data'對象創建的String。在代碼中,您將其轉換回「Data」並再次返回到「String」。這非常麻煩。您可以在一行中創建來自原始'Data'對象的十六進制表示'let enCodedUTF8String = data.map {String(format:「%02x」,$ 0)} .joined()' – vadian

回答

0

評論...

//I am Getting error here i.e C-style for statement has been removed in Swift 3 
for var index = trimmedString.startIndex; index < trimmedString.endIndex; index = Collection.index(after: Collection.index(after: index)) { 
    let byteString = trimmedString.substring(with: (index ..< Collection.index(after: Collection.index(after: index)))) 
    let num = UInt8(byteString.withCString { strtoul($0, nil, 16) }) 
    data?.append([num] as [UInt8], length: 1) 
} 

正在遍歷trimmedString的索引,但要去2?一次?

如果index爲0(這是開始),那麼接下來指數將... 0之後

所以2 1後指數後指數... 2後

指數?那麼4?那麼6?

然後你得到從該索引到下一個索引的子字符串?

然後你從它創建一個UInt8,並將它附加一些數據...

所以......

我想最簡單的變化將是一個while環......

var index = trimmedString.startIndex 

while index < trimmedString.endIndex { 
    // note I used the ... for the range to avoid the repeated... index after index after index 
    let byteString = trimmedString.substring(with: (index ... Collection.index(after: index))) 
    let num = UInt8(byteString.withCString { strtoul($0, nil, 16) }) 
    data?.append([num] as [UInt8], length: 1) 

    // check the docs, there are easier ways of moving the index by more than one place. 
    index = Collection(index, offsetBy: 2) 
} 

像這樣的東西應該作爲一個初步的變化。

通過@vadian

至於建議更好的方法指出了@vadian有一個更簡單,更優雅做這一切在一行代碼的方式...

let enCodedUTF8String = data.map{ String(format: "%02x", $0) }.joined() 

我認爲會是這樣,但我無法自己想出來。