inout String在Swift 3中不能转换为String

时间:2017-09-05 07:08:24

标签: ios swift3

我有以下代码在Swift 2中正常工作:

jsonObject = [
                "AppId": fetchQueryResult[0].appId,
                "MobileNo": String(
                    data: try RNDecryptor.decryptData(self.fetchQueryResult[0].userMobileNumber.dataFromHexString() as Data!, withPassword: Constants.ENCRYPTION_KEY),
                    encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))! as String,

                "DeviceType": Constants.DEVICE_TYPE,
                "AppVersion": fetchQueryResult[0].appVersion,
                "Password": NSString(data: try RNDecryptor.decryptData(self.fetchQueryResult[0].password.dataFromHexString(), withPassword: Constants.ENCRYPTION_KEY), encoding: String.Encoding.utf8)! as String
            ]

我正在将代码升级到Swift 3,然而,我收到了一个错误:

  

'inout String'不能转换为'String'类型

它在这一行上出错:

"MobileNo" : String(data .....)

error with context

有关详细信息,我正在编写函数dataFromHexString()

extension String {
    subscript (i : Int) -> Character {
        return self[self.characters.index(self.startIndex, offsetBy: i)]
    }

    mutating func dataFromHexString() -> NSMutableData {
        let data = NSMutableData(reserveCapacity(characters.count / 2))
        let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
        regex.enumerateMatches(in: self, options: [], range: NSMakeRange(0, characters.count)) {
            match, flags,stop in
            let byteString = (self as NSString).substring(with: match!.range)
            let num = UInt8(byteString.withCString {strtoul($0, nil, 16)})
            data.append([num], length: 1)
        }
        return data
    }
}

我是iOS新手。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您应该将作业包装在 do / catch块中,以正确处理任何异常。

do {
    jsonObject = [
                    "AppId": fetchQueryResult[0].appId,
                    "MobileNo": String(
                        data: try RNDecryptor.decryptData(self.fetchQueryResult[0].userMobileNumber.dataFromHexString() as Data!, withPassword: Constants.ENCRYPTION_KEY),
                        encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))! as String,

                    "DeviceType": Constants.DEVICE_TYPE,
                    "AppVersion": fetchQueryResult[0].appVersion,
                    "Password": NSString(data: try RNDecryptor.decryptData(self.fetchQueryResult[0].password.dataFromHexString(), withPassword: Constants.ENCRYPTION_KEY), encoding: String.Encoding.utf8)! as String
                ]
} catch let error {
    print(error.localizedDescription)
}

答案 1 :(得分:0)

请检查以下代码:

mutating func dataFromHexString() -> NSMutableData {
    let data = NSMutableData(reserveCapacity(characters.count / 2))
    let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
    regex.enumerateMatches(in: self, options: [], range: characters.startIndex..<characters.endIndex) {
        match, flags,stop in
        let byteString = (self as NSString).substring(with: match!.range)
        let num = UInt8(byteString.withCString {strtoul($0, nil, 16)})
        data.append([num], length: 1)
    }
    return data
}