私はprotobuf v2 in Swiftを持っています。私はそれを別のprotobufに追加しようとしています。スイフトにプロトコルバッファを追加するには?
malloc: *** mach_vm_map(size=2749415424) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
を多分それは私の読書が帰って:
let attachment = getAttachment(id: 987) //From cloud database
var protosData = NSMutableData(data: attachment)
items.forEach { //Some struct array of values
guard let proto = try? MyProtoBuf.Builder()
.setEpochMillis($0.date.epochMilliseconds)
.setValue($0.value)
.build() else { return }
protosData.appendData(proto.data())
}
saveAttachment(protosData) //Store to cloud
私がそれを読み戻すときに私はこのエラーを取得するので、私はデータを破損てるようだようだ:これは私がしようとしているものです値は、ここでは、間違っている私はストレージから追加されたデータを読み取るためにやってるものです:
:extension GeneratedMessageProtocol {
static func getStreamData(data: NSData) -> [Self] {
var messages = [Self]()
do {
let inStream = NSInputStream(data:data)
inStream.open()
defer { inStream.close() }
while inStream.hasBytesAvailable {
var sizeBuffer: [UInt8] = [0,0,0,0]
inStream.read(&sizeBuffer, maxLength: sizeBuffer.count)
let data = NSData(bytes: sizeBuffer, length: sizeBuffer.count)
let messageSize = data.uint32.littleEndian
var buffer = Array<UInt8>(count: Int(messageSize), repeatedValue: 0)
inStream.read(&buffer, maxLength: Int(messageSize))
let messageData = NSData(bytes: buffer, length:Int(messageSize))
messages.append(try self.parseFromData(messageData))
}
}
catch {
}
return messages
}
}
extension NSData {
var uint32: UInt32 {
get {
var number: UInt32 = 0
self.getBytes(&number, length: sizeof(UInt32))
return number
}
}
}
そしてここでは、私のいるProtobufメッセージです
syntax = "proto2";
message MyProtoBuf {
optional uint64 epochMillis = 1;
optional uint32 value = 2;
}
既存のprotobufにデータを追加する正しい方法は、配列アイテムを1つずつ解析し、protobufを追加してから、配列全体をバイトに変換する必要はありませんか?
おそらく区切り記号がありませんか? – TruMan1