sdk-objectmodel-swift
使用 swift 实现了 XYO 对象模型。
分支 | 状态 |
---|---|
Master | |
Develop |
入门
该库结构包含 4 个中心对象,您可能会使用它们:缓存、结构、可迭代结构,以及模式。
缓存
XyoBuffer 类是一个工具,用于创建字节数据缓冲区,您可以轻松放置数字、模式和其他数据。
创建缓存
您可以从无开始创建缓存,从另一个缓存,或从另一个缓存的子集中创建。
let emptyBuffer = XyoBuffer() // creates an empty buffer
let byteFilledBuffer = XyoBuffer(data: [0x13, 0x37]) // creates a buffer with 0x1337 inside of it
let subBufferFromBytes = XyoBuffer(data: [0x00, 0x01, 0x02, 0x03], allowedOffset: 1, lastOffset: 3) // creates a buffer with the value 0x0102
let subBufferFromBuffer = XyoBuffer(data: XyoBuffer(...), allowedOffset: 1, lastOffset: 3) // creates a buffer with from range 1 to 2
将数据添加到缓冲区
创建缓冲区后,你可以将其末尾追加其他数据。
let buffer = XyoBuffer()
buffer.put(schema: XyoObjectSchema(0, 0)) // puts a schema into the buffer
buffer.put(bytes: [0x13, 0x37]) // puts 0x1337 into the buffer
buffer.put(bits: UInt8(5)) // puts a single byte into the buffer
buffer.put(bits: UInt16(5)) // puts a short into the buffer
buffer.put(bits: UInt32(5)) // puts a int into the buffer
buffer.put(bits: UInt62(5)) // puts a long into the buffer
buffer.put(buffer: XyoBuffer(...)) // puts a buffer into the buffer
从缓冲区获取数据
当缓冲区中包含数据后,你可以从中提取有意义的数据。
let schema = buffer.getSchema(offset: 2) // gets a schema at offset 2
let number = buffer.getUInt8(offset: 2) // gets a byte at offset 2
let number = buffer.getUInt16(offset: 2) // gets a short at offset 2
let number = buffer.getUInt32(offset: 2) // gets a int at offset 2
let number = buffer.getUInt64(offset: 2) // gets a long at offset 2
let subBuffer = buffer.copyRangeOf(from: 2, to: 4) // gets a range between 2 and 4
模式
XyoObjectSchema 结构体是一个用于包含某种对象编码信息的对象。模式被分解为两个字节,编码目录和id。编码目录包括大小、是否可迭代、是否为类型化可迭代和其id。id是模式的简单id。
创建模式
你可以从编码目录和id创建模式,从配置参数或从字节中创建。
let manualSchema = XyoObjectSchema(id : 12, encodingCatalogue : 20) // creates a schema with id 12, and encodingCatalogue 20
let configSchema = XyoObjectSchema.create(id : 12, isIterable : true, isTypedIterable: false, sizeIdentifier : XyoObjectSizes.TWO) // create an iteratble untyped schema with id 12, and a two byte size.
读取模式
创建模式后,可以从中读取以获取关于后续对象的信息。
let id = schema.id // gets the id
let isTyped = schema.getIsTypedIterable() // gets if the schema is typed
let isIterable = schema.getIsIterable() // gets if the schema is iterable
let sizeOfSize = schema.getSizeIdentifier() // gets the size of the size
结构
结构是一个包含模式、大小和值的字节数据缓冲区。XyoObjectStructure类允许用户轻松创建和读取XYO结构。
创建一个结构
您可以从模式和价值或另一个结构的字节数据中创建一个结构。
let custumSchema = XyoObjectSchema(id: 12, encodingCatalogue: 0)
let custumStructure = XyoObjectStructure.newInstance(schema: custumSchema, bytes: [0x13, 0x37]) // creates a schema with the value 0x1337
let schemaBytes : [UInt8] = [0x00, 0x01, 0x03, 0x13, 0x37]
let createdStructure = XyoObjectStructure(value: schemaBytes)
读取结构
结构创建后,可以从它中提取出值、模式和大小。
let structureSchema = structure.getSchema() // gets the schema of the structure
let structureValue = structure.getValue() // gets the value of the structure
let entireStructure = structure.getBuffer() // gets the entire buffer value of the structure
let structureSize = structure.getSize() // gets the size of the buffer, not including the schema
可迭代结构
可迭代结构是一种特殊的结构,可以迭代,例如链表或数组。可迭代结构有两种类型:类型化和无类型。类型化可迭代结构更小,但所有元素都必须遵循单个模式。无类型数组更大,但所有元素不必须共享相同的模式。
创建可迭代结构
您可以从缓冲区或从元素集合中创建一个可迭代结构。
let custumSchema : XyoObjectSchema = ...
let elements : [XyoObjectStructure] = [...]
let untyped = XyoIterableStructure.encodeUntypedIterableObject(schema: custumSchema, values: elements) // creates an untyped array of the elements
let typed = XyoIterableStructure.encodeTypedIterableObject(schema: custumSchema, values: elements) // creates an typed array of the elements
let fromBytes = XyoObjectStructure(value : XyoBuffer(data: ...))
从可迭代结构中读取
可迭代结构创建后,可以从中提取出所有元素。
// using an iterator
let iterator = iterableStructure.getNewIterator() // gets a fresh iterator
while (try iterator.hasNext()) {
doSomethingWithItem (try iterator.next())
}
let itemTwo = iterableStructure.get(index: 1) // gets an item at index 1
let count = iterableStructure.getCount() // gets the total number of elements (size)
// you can also get all of the elements in the structure that have an id
let groupOfElementsOfId = iterableStructure.get(id: 0x02) // will get all the elements in the set with id: 0x02
许可证
本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件