diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index 6afa069ea..8257c0c12 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -725,9 +725,9 @@ class SwiftGenerator : public BaseGenerator { code_.SetValue("CONSTANT", default_value); code_.SetValue("VALUETYPE", "Bool"); code_ += GenReaderMainBody(optional) + "\\"; - code_.SetValue("VALUETYPE", "Byte"); - code_ += GenOffset() + "return o == 0 ? {{CONSTANT}} : 0 != " + - GenReader("VALUETYPE", "o") + " }"; + code_ += GenOffset() + + "return o == 0 ? {{CONSTANT}} : " + GenReader("VALUETYPE", "o") + + " }"; if (parser_.opts.mutable_buffer) code_ += GenMutate("o", GenOffset()); return; } diff --git a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/FlatBuffersMonsterWriterTests.swift b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/FlatBuffersMonsterWriterTests.swift index adc918a5d..b00094e2a 100644 --- a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/FlatBuffersMonsterWriterTests.swift +++ b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/FlatBuffersMonsterWriterTests.swift @@ -217,6 +217,10 @@ class FlatBuffersMonsterWriterTests: XCTestCase { XCTAssertEqual(monster.testType, .monster) + XCTAssertTrue(monster.mutate(testbool: false)) + XCTAssertEqual(monster.testbool, false) + XCTAssertTrue(monster.mutate(testbool: true)) + XCTAssertEqual(monster.mutate(inventory: 1, at: 0), true) XCTAssertEqual(monster.mutate(inventory: 2, at: 1), true) XCTAssertEqual(monster.mutate(inventory: 3, at: 2), true) diff --git a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift index b11c142d7..a295a07b8 100644 --- a/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift +++ b/tests/swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests/monster_test_generated.swift @@ -1221,8 +1221,8 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } - public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : 0 != _accessor.readBuffer(of: Byte.self, at: o) } - @discardableResult public func mutate(testbool: Byte) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } + public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } + @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } public var testhashs32Fnv1: Int32 { let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int32.self, at: o) } @discardableResult public func mutate(testhashs32Fnv1: Int32) -> Bool {let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return _accessor.mutate(testhashs32Fnv1, index: o) } public var testhashu32Fnv1: UInt32 { let o = _accessor.offset(VTOFFSET.testhashu32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt32.self, at: o) } diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift index b11c142d7..a295a07b8 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift @@ -1221,8 +1221,8 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.postion)) } - public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : 0 != _accessor.readBuffer(of: Byte.self, at: o) } - @discardableResult public func mutate(testbool: Byte) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } + public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } + @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } public var testhashs32Fnv1: Int32 { let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: Int32.self, at: o) } @discardableResult public func mutate(testhashs32Fnv1: Int32) -> Bool {let o = _accessor.offset(VTOFFSET.testhashs32Fnv1.v); return _accessor.mutate(testhashs32Fnv1, index: o) } public var testhashu32Fnv1: UInt32 { let o = _accessor.offset(VTOFFSET.testhashu32Fnv1.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt32.self, at: o) } diff --git a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/optional_scalars_generated.swift b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/optional_scalars_generated.swift index 74585a170..8758b1ef3 100644 --- a/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/optional_scalars_generated.swift +++ b/tests/swift/tests/Tests/FlatBuffers.Test.SwiftTests/optional_scalars_generated.swift @@ -109,9 +109,9 @@ public struct optional_scalars_ScalarStuff: FlatBufferObject, Verifiable { public var justF64: Double { let o = _accessor.offset(VTOFFSET.justF64.v); return o == 0 ? 0.0 : _accessor.readBuffer(of: Double.self, at: o) } public var maybeF64: Double? { let o = _accessor.offset(VTOFFSET.maybeF64.v); return o == 0 ? nil : _accessor.readBuffer(of: Double.self, at: o) } public var defaultF64: Double { let o = _accessor.offset(VTOFFSET.defaultF64.v); return o == 0 ? 42.0 : _accessor.readBuffer(of: Double.self, at: o) } - public var justBool: Bool { let o = _accessor.offset(VTOFFSET.justBool.v); return o == 0 ? false : 0 != _accessor.readBuffer(of: Byte.self, at: o) } - public var maybeBool: Bool? { let o = _accessor.offset(VTOFFSET.maybeBool.v); return o == 0 ? nil : 0 != _accessor.readBuffer(of: Byte.self, at: o) } - public var defaultBool: Bool { let o = _accessor.offset(VTOFFSET.defaultBool.v); return o == 0 ? true : 0 != _accessor.readBuffer(of: Byte.self, at: o) } + public var justBool: Bool { let o = _accessor.offset(VTOFFSET.justBool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } + public var maybeBool: Bool? { let o = _accessor.offset(VTOFFSET.maybeBool.v); return o == 0 ? nil : _accessor.readBuffer(of: Bool.self, at: o) } + public var defaultBool: Bool { let o = _accessor.offset(VTOFFSET.defaultBool.v); return o == 0 ? true : _accessor.readBuffer(of: Bool.self, at: o) } public var justEnum: optional_scalars_OptionalByte { let o = _accessor.offset(VTOFFSET.justEnum.v); return o == 0 ? .none_ : optional_scalars_OptionalByte(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .none_ } public var maybeEnum: optional_scalars_OptionalByte? { let o = _accessor.offset(VTOFFSET.maybeEnum.v); return o == 0 ? nil : optional_scalars_OptionalByte(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? nil } public var defaultEnum: optional_scalars_OptionalByte { let o = _accessor.offset(VTOFFSET.defaultEnum.v); return o == 0 ? .one : optional_scalars_OptionalByte(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .one }