나는 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를 추가 한 다음 전체 배열을 바이트로 다시 변환하지 않고 기존 protobuf에 데이터를 추가하는 올바른 방법은 무엇입니까?
아마 구분 기호가 누락 되었습니까? – TruMan1