gpt4 book ai didi

ios - '子字符串(来自 : )' is deprecated: Please use String slicing subscript with a ' partial range from' operator. Swift 4 错误

转载 作者:可可西里 更新时间:2023-11-01 01:10:41 26 4
gpt4 key购买 nike

我正在将我现有的应用程序从 Swift 3 转换为 Swift 4。它给出了错误:

'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.

'characters' is deprecated: Please use String or Substring directly

我的 Swift 3 代码是:

public convenience init?(hexString: String, alpha: Float) {
var hex = hexString

// Check for hash and remove the hash
if hex.hasPrefix("#") {
hex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 1))
}

if (hex.range(of: "(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)", options: .regularExpression) != nil) {

// Deal with 3 character Hex strings
if hex.characters.count == 3 {
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 1))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 1) ..< hex.characters.index(hex.startIndex, offsetBy: 2)))
let blueHex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 2))

hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex
}

let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 2))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 2) ..< hex.characters.index(hex.startIndex, offsetBy: 4)))
let blueHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 4) ..< hex.characters.index(hex.startIndex, offsetBy: 6)))

var redInt: CUnsignedInt = 0
var greenInt: CUnsignedInt = 0
var blueInt: CUnsignedInt = 0

Scanner(string: redHex).scanHexInt32(&redInt)
Scanner(string: greenHex).scanHexInt32(&greenInt)
Scanner(string: blueHex).scanHexInt32(&blueInt)

self.init(red: CGFloat(redInt) / 255.0, green: CGFloat(greenInt) / 255.0, blue: CGFloat(blueInt) / 255.0, alpha: CGFloat(alpha))
}
else {
self.init()
return nil
}
}

如何将其转换为 Swift 4

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com