Swift4.0 strtoul、strtod、strtof、strtol等字符串函数的改变

前端之家收集整理的这篇文章主要介绍了Swift4.0 strtoul、strtod、strtof、strtol等字符串函数的改变前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在新的iOS 11的SDK中,这几个函数参数类型发生了改变,

public func strtod(_: UnsafePointer<Int8>!,_: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!) -> Double
public func strtof(_: UnsafePointer<Int8>!,_: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!) -> Float
public func strtol(_ __str: UnsafePointer<Int8>!,_ __endptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!,_ __base: Int32) -> Int

public func strtoll(_ __str: UnsafePointer<Int8>!,_ __base: Int32) -> Int64
/* !__DARWIN_NO_LONG_LONG */
public func strtoul(_ __str: UnsafePointer<Int8>!,_ __base: Int32) -> UInt

public func strtoull(_ __str: UnsafePointer<Int8>!,_ __base: Int32) -> UInt64

从原来可以直接传的char*变成了一个UnsafePointer<Int8>。那么我们用到函数的地方也要改变一下,具体代码如下:

let s = hex[hex.characters.index(hex.startIndex,offsetBy: start)..<hex.characters.index(hex.startIndex,offsetBy: end)]
return CGFloat(s.withCString{strtoul($0,nil,16)})

这里的s是一个String类型,他有一个withCString方法。利用这个方法可以将字符串中的字符转换成 UnsafePointer<Int8>类型 原文链接:https://www.f2er.com/swift/320993.html

猜你在找的Swift相关文章