i'm working on building custom file opener in ios swift shapefiles (a gis format, not particularly relevant question). these files have header 100 bytes long. i'm able read 4-byte arrays, store information want. can convert these arrays swift types data
, nsdata
, , have few other options transforming them (like base64encodedstring
). i'm having trouble converting these raw arrays or data or of formats useful attributes 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" } }
this returns because testing reasons having return string
how can open files, , read raw bytes types (doubles
, ints
, strings
) in swift?
you can follow:
to convert string, int or double data:
xcode 9 • swift 4
extension string { var data: data { return data(utf8) } } extension binaryinteger { // or extension integer swift 3.x var data: data { var source = self return data(bytes: &source, count: memorylayout<self>.size) // return 4 bytes 32-bit integers , 8 bytes 64-bit integers } } extension floatingpoint { var data: data { var source = self return data(bytes: &source, count: memorylayout<self>.size) // return 4 bytes single-precision, 8 bytes double-precision , 16 bytes extended precision floating point types } }
to convert data string, int or double:
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) ?? "" } }
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 // 9223372036854775808
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"
Comments
Post a Comment