A memory stream tool developed by Swift 3 working on iOS, macOS and Linux.
你可以在「这里」找到中文介绍。
EMDataStream depend on 「EMFileStream」.
Clone EMFileStream and this repo and copy the framework to your project.
Import the framework
import EMDataStreamCreate a stream like this:
let stream = try EMDataStream.init(size: 10)
//let stream = try EMDataStream.init() means size = 0
//let stream = try EMDataStream.init(data: Data)Write some data:
try stream.write(string: name, writeSize: 20)
try stream.write(int: age)
try stream.write(float: source)
try stream.write(string: memo, writeSize: 100)Seek to some position:
try stream.seek(toPosition: 10)
//Absolute deviation
try stream.seek(byPosition: 10)
//Relative deviationTo Data
let data = stream.toData()Create a stream like this:
let stream = try EMDataStream.init(size: 10)Read some data:
let name = try stream.readString(withSize: 20)
let age = try stream.readInt()
let source = try stream.readFloat()
let memo = try stream.readString(withSize: 100)Use this two protocols:
public protocol EMDataStreamReadable {
init(stream: EMDataStream) throws
}
public protocol EMDataStreamWriteable {
func emObjectWrite(withStream stream: EMDataStream) throws
}Then you can archive your own object to Data with EMDataStream!
There is a Demo:
class Student: EMDataStreamReadable, EMDataStreamWriteable {
var name: String
var age: Int
var source: Float
var memo: String
init(name: String, age: Int, source: Float, memo: String) {
self.name = name
self.age = age
self.source = source
self.memo = memo
}
public required init(stream: EMDataStream) throws {
self.name = try stream.readString(withSize: 20)
self.age = try stream.readInt()
self.source = try stream.readFloat()
self.memo = try stream.readString(withSize: 100)
}
func emObjectWrite(withStream stream: EMDataStream) throws {
try stream.write(string: name, writeSize: 20)
try stream.write(int: age)
try stream.write(float: source)
try stream.write(string: memo, writeSize: 100)
}
}Then you can archive and unarchive your object like this
let student = Student.init(name: "Sark", age: 20, source: 78.9, memo: "Memo..........")
do {
//archive your object
let stream = EMDataStream.init()
try stream.write(object: student)
//unarchive your object
let streamOut = EMDataStream.init(data: stream.toData())
let stundentOut: Student = try streamOut.readObject()
//!!!The type of `studentOut` have to be specified
print(stundentOut)
} catch {
print(error)
}