Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在 swift中有一系列角色: static let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"] 我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将charArray [0]转换为UInt8? 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显 我在 swift中有一系列角色:

static let chararray: [Character] = [ "S","t","r","i","n","g"]

我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将chararray [0]转换为UInt8?

解决方法 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显式初始化数组. [UInt8]转换;因为String.UTF8VIEw(来自String.utf8)是一个CollectionType,你可以对String.UTF8VIEw本身应用一个map *** 作;使用UInt8初始化.即,

let chararray: [Character] = [ "S","g"]let asUInt8Array = String(chararray).utf8.map{ UInt8(
extension SequenceType where Generator.Element == Character {    /* extension accessible as function */    func asByteArray() -> [UInt8] {        return String(self).utf8.map{UInt8(
let chararray: [Character] = [ "S","g"]/* use extension function */let asUInt8Array = chararray.asByteArray()/* or computed property */let asUInt8Array = chararray.byteArray
)} } /* or,as @LeoDabus pointed out below (thanks!),use a computed property for this simple case */ var byteArray : [UInt8] { return String(self).utf8.map{UInt8([+++])} }}
) }print(asUInt8Array)/* [83,116,114,105,110,103] */print(asUInt8Array.dynamicType)/* Array<UInt8> */

关于你的下面的评论(“与Swift的抽象相比,与Objective-C的简单方式相比受挫”):如果你认为上面的内容很混乱,你可以将它包含在SequenceType的扩展中,约束到Character元素,允许在实践中更容易使用.例如.:

[+++]

用法示例:

[+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将Character转换为UInt8全部内容,希望文章能够帮你解决如何在Swift中将Character转换为UInt8所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在 swift中有一系列角色: static let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"] 我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将charArray [0]转换为UInt8? 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显 我在 swift中有一系列角色:

static let chararray: [Character] = [ "S","t","r","i","n","g"]

我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将chararray [0]转换为UInt8?

解决方法 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显式初始化数组. [UInt8]转换;因为String.UTF8VIEw(来自String.utf8)是一个CollectionType,你可以对String.UTF8VIEw本身应用一个map *** 作;使用UInt8初始化.即,

let chararray: [Character] = [ "S","g"]let asUInt8Array = String(chararray).utf8.map{ UInt8(
extension SequenceType where Generator.Element == Character {    /* extension accessible as function */    func asByteArray() -> [UInt8] {        return String(self).utf8.map{UInt8(
let chararray: [Character] = [ "S","g"]/* use extension function */let asUInt8Array = chararray.asByteArray()/* or computed property */let asUInt8Array = chararray.byteArray
)} } /* or,as @LeoDabus pointed out below (thanks!),use a computed property for this simple case */ var byteArray : [UInt8] { return String(self).utf8.map{UInt8()} }}
) }print(asUInt8Array)/* [83,116,114,105,110,103] */print(asUInt8Array.dynamicType)/* Array<UInt8> */

关于你的下面的评论(“与Swift的抽象相比,与Objective-C的简单方式相比受挫”):如果你认为上面的内容很混乱,你可以将它包含在SequenceType的扩展中,约束到Character元素,允许在实践中更容易使用.例如.:

[+++]

用法示例:

[+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将Character转换为UInt8全部内容,希望文章能够帮你解决如何在Swift中将Character转换为UInt8所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我在 swift中有一系列角色: static let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"] 我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将charArray [0]转换为UInt8? 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显 我在 swift中有一系列角色:

static let chararray: [Character] = [ "S","t","r","i","n","g"]

我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将chararray [0]转换为UInt8?

解决方法 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显式初始化数组. [UInt8]转换;因为String.UTF8VIEw(来自String.utf8)是一个CollectionType,你可以对String.UTF8VIEw本身应用一个map *** 作;使用UInt8初始化.即,

let chararray: [Character] = [ "S","g"]let asUInt8Array = String(chararray).utf8.map{ UInt8(
extension SequenceType where Generator.Element == Character {    /* extension accessible as function */    func asByteArray() -> [UInt8] {        return String(self).utf8.map{UInt8(
let chararray: [Character] = [ "S","g"]/* use extension function */let asUInt8Array = chararray.asByteArray()/* or computed property */let asUInt8Array = chararray.byteArray
)} } /* or,as @LeoDabus pointed out below (thanks!),use a computed property for this simple case */ var byteArray : [UInt8] { return String(self).utf8.map{UInt8()} }}
) }print(asUInt8Array)/* [83,116,114,105,110,103] */print(asUInt8Array.dynamicType)/* Array<UInt8> */

关于你的下面的评论(“与Swift的抽象相比,与Objective-C的简单方式相比受挫”):如果你认为上面的内容很混乱,你可以将它包含在SequenceType的扩展中,约束到Character元素,允许在实践中更容易使用.例如.:

用法示例:

[+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将Character转换为UInt8全部内容,希望文章能够帮你解决如何在Swift中将Character转换为UInt8所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
如何在Swift中将Character转换为UInt8_app_内存溢出

如何在Swift中将Character转换为UInt8

如何在Swift中将Character转换为UInt8,第1张

概述我在 swift中有一系列角色: static let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"] 我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将charArray [0]转换为UInt8? 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显 我在 swift中有一系列角色:

static let chararray: [Character] = [ "S","t","r","i","n","g"]

我想对每个字符进行一些按字节 *** 作作为字节(UInt8).例如,如何将chararray [0]转换为UInt8?

解决方法 您需要通过Character:s的String表示转换为UInt8.但是,您无需在[Character] – >中显式初始化数组. [UInt8]转换;因为String.UTF8VIEw(来自String.utf8)是一个CollectionType,你可以对String.UTF8VIEw本身应用一个map *** 作;使用UInt8初始化.即,

let chararray: [Character] = [ "S","g"]let asUInt8Array = String(chararray).utf8.map{ UInt8(
extension SequenceType where Generator.Element == Character {    /* extension accessible as function */    func asByteArray() -> [UInt8] {        return String(self).utf8.map{UInt8(
let chararray: [Character] = [ "S","g"]/* use extension function */let asUInt8Array = chararray.asByteArray()/* or computed property */let asUInt8Array = chararray.byteArray
)} } /* or,as @LeoDabus pointed out below (thanks!),use a computed property for this simple case */ var byteArray : [UInt8] { return String(self).utf8.map{UInt8()} }}
) }print(asUInt8Array)/* [83,116,114,105,110,103] */print(asUInt8Array.dynamicType)/* Array<UInt8> */

关于你的下面的评论(“与Swift的抽象相比,与Objective-C的简单方式相比受挫”):如果你认为上面的内容很混乱,你可以将它包含在SequenceType的扩展中,约束到Character元素,允许在实践中更容易使用.例如.:

用法示例:

总结

以上是内存溢出为你收集整理的如何在Swift中将Character转换为UInt8全部内容,希望文章能够帮你解决如何在Swift中将Character转换为UInt8所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1013952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存