简体   繁体   English

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

[英]How can I convert data into types like Doubles, Ints and Strings in Swift?

I'm working on building a custom file opener in iOS Swift for shapefiles (a GIS format, not particularly relevant to this question). 我正在为iOS Swift中的shapefile构建一个自定义文件打开器(一种GIS格式,与此问题无关)。 These files have a header which is 100 bytes long. 这些文件有一个100字节长的标题。 I'm able to read this into 4-byte arrays, which store information I want. 我能够将其读入4字节数组,存储我想要的信息。 I can convert these arrays into the Swift types Data and NSData , and have a few other options for transforming them (like Base64EncodedString ). 我可以将这些数组转换为Swift类型DataNSData ,并有一些其他选项可以转换它们(如Base64EncodedString )。 But I'm having trouble converting these raw arrays or the Data or any of the formats into useful attributes like Double , Int , and String . 但是我无法将这些原始数组或数据或任何格式转换为有用的属性,如DoubleIntString

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"
    }
}

This currently only returns A because for testing reasons I am having it return a string 这当前只返回A,因为测试原因我返回一个字符串

How can I open files, and read their raw bytes into types ( Doubles , Ints , Strings ) in Swift? 如何打开文件,并将其原始字节读入Swift中的类型( DoublesIntsStrings )?

You can do it as follow: 你可以这样做:

To convert from String, Int or Double to Data: 要从String,Int或Double转换为数据:

Xcode 9 • Swift 4 // for old Swift 3 syntax click here Xcode 9•Swift 4 //对于旧的Swift 3语法,请单击此处

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)  
    }
}

To convert from Data back to String, Int or Double: 要从Data转换回String,Int或Double:

Swift 4.2 or earlier Swift 4.2或更早版本

extension Data {
    var integer: Int {
        return withUnsafeBytes { $0.pointee }
    }
    var int32: Int32 {
        return withUnsafeBytes { $0.pointee }
    }
    var float: Float {
        return withUnsafeBytes { $0.pointee }
    }
    var float80: Float80 {
        return withUnsafeBytes { $0.pointee }
    }
    var double: Double {
        return withUnsafeBytes { $0.pointee }
    }
    var string: String? {
        return String(data: self, encoding: .utf8)
    }
}

edit/update Swift 5 编辑/更新Swift 5

extension Data {
    var integer: Int {
        return withUnsafeBytes { $0.load(as: Int.self) }
    }
    var int32: Int32 {
        return withUnsafeBytes { $0.load(as: Int32.self) }
    }
    var float: Float {
        return withUnsafeBytes { $0.load(as: Float.self) }
    }
    var float80: Float80 {
        return withUnsafeBytes { $0.load(as: Float80.self) }
    }
    var double: Double {
        return withUnsafeBytes { $0.load(as: Double.self) }
    }
    var string: String? {
        return String(data: self, encoding: .utf8)
    }
}

Playground testing 游乐场测试


let intData = 1_234_567_890_123_456_789.data    // 8 bytes (64 bit Integer)
let dataToInt = intData.integer                 // 1234567890123456789

let intMinData = Int.min.data                   // 8 bytes (64 bit Integer)
let backToIntMin = intMinData.integer           // -9223372036854775808

let intMaxData = Int.max.data                   // 8 bytes (64 bit Integer)
let backToIntMax = intMaxData.integer           // 9223372036854775807

let myInt32Data = Int32(1_234_567_890).data     // 4 bytes (32 bit Integer)
let backToInt32 = myInt32Data.int32             // 1234567890

let int32MinData = Int32.min.data               // 4 bytes (32 bit Integer)
let backToInt32Min = int32MinData.int32         // -2147483648

let int32MaxData = Int32.max.data               // 4 bytes (32 bit Integer)
let backToInt32Max = int32MaxData.int32         // 2147483647

let myFloatData = Float.pi.data                 // 4 bytes (32 bit single=precison FloatingPoint)
let backToFloat = myFloatData.float             // 3.141593
backToFloat == .pi      // true

let myDoubleData = Double.pi.data               // 8 bytes (64 bit double-precision FloatingPoint)
let backToDouble = myDoubleData.double          // 3.141592653589793
backToDouble == .pi     // true

let myFloat80Data = Float80.pi.data             // 16 bytes (128 bit extended-precision FloatingPoint)
let backToFloat80 = myFloat80Data.float80       // 3.141592653589793116
backToFloat80 == .pi    // true

let myStringData = Data("Hello World !!!".data.prefix(4))   // 4 bytes
let backToString = myStringData.string                      //  "Hell"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM