Error[8]: Undefined offset: 7, 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(

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { [+++].pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

[+++] [+++] [+++] [+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, 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: 8, 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(

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { .pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

[+++] [+++] [+++] [+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, 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: 9, 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(

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { .pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

[+++] [+++] [+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, 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: 10, 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(

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { .pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

[+++] [+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, 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: 11, 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(

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { .pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

[+++] 总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, 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中将数据转换为双打,Ints和字符串等类型?_app_内存溢出

如何在Swift中将数据转换为双打,Ints和字符串等类型?

如何在Swift中将数据转换为双打,Ints和字符串等类型?,第1张

概述我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关).这些文件有一个100字节长的标题.我能够将其读入4字节数组,存储我想要的信息.我可以将这些数组转换为Swift类型Data和NSData,并有一些其他选项可以转换它们(如Base64EncodedString).但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如Double,Int和String.

import Foundation    struct ShapeReader {        var shapefile = fileHandle(forReadingAtPath: "/Users/christopherjlowrIE/documents/Shapes/SF_Neighborhoods/Planning_Zones.shp")        var fileheader: String{            let header = shapefile?.readData(ofLength: 100)            let headerStream = inputStream(data: header!)            headerStream.open()            var buffer = [UInt8](repeating: 0,count: 4)            while (headerStream.hasBytesAvailable){                headerStream.read(&buffer,maxLength: buffer.count)                print(buffer)                let x = Data(buffer)                print(x)        }        return "A"    }}

这当前只返回A,因为测试原因我返回一个字符串

如何打开文件,并将其原始字节读入Swift中的类型(双打,Ints,字符串)?

解决方法 你可以这样做:

要从String,Int或Double转换为数据:

Xcode 9•Swift 4 //对于旧的Swift 3语法单击here

extension String {    var data: Data { return Data(utf8) }}extension Numeric {    var data: Data {        var source = self        // This will return 1 byte for 8-bit,2 bytes for 16-bit,4 bytes for 32-bit and 8 bytes for 64-bit binary integers. For floating point types it will return 4 bytes for single-precision,8 bytes for double-precision and 16 bytes for extended precision.        return Data(bytes: &source,count: MemoryLayout<Self>.size)      }}

要从Data转换回String,Int或Double:

extension Data {    var integer: Int {        return withUnsafeBytes { 
let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)let dataToInt = intData.integer                 // 1234567890123456789let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)let backToIntMin = intMinData.integer           // -9223372036854775808let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)let backToIntMax = intMaxData.integer           // 9223372036854775807
.pointee } } var int32: Int32 { return withUnsafeBytes {
let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)let backToInt32 = myInt32Data.int32             // 1234567890let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)let backToInt32Min = int32MinData.int32         // -2147483648let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)let backToInt32Max = int32MaxData.int32         // 2147483647
.pointee } } var float: float { return withUnsafeBytes {
let myfloatData = float.pi.data                 // 4 bytes (32 bit single=precison floatingPoint)let backTofloat = myfloatData.float             // 3.141593backTofloat == .pi      // truelet myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision floatingPoint)let backTodouble = myDoubleData.double          // 3.141592653589793backTodouble == .pi     // truelet myfloat80Data = float80.pi.data             // 16 bytes (128 bit extended-precision floatingPoint)let backTofloat80 = myfloat80Data.float80       // 3.141592653589793116backTofloat80 == .pi    // true
.pointee } } var float80: float80 { return withUnsafeBytes {
let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 byteslet backToString = myStringData.string                      //  "Hell"
.pointee } } var double: Double { return withUnsafeBytes { .pointee } } var string: String { return String(data: self,enCoding: .utf8) ?? "" }}

游乐场测试

总结

以上是内存溢出为你收集整理的如何在Swift中将数据转换为双打,Ints和字符串等类型?全部内容,希望文章能够帮你解决如何在Swift中将数据转换为双打,Ints和字符串等类型?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存