Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift] Update tutorial for getRootAs -> getCheckedRoot #8409

Open
peeeeter opened this issue Sep 27, 2024 · 6 comments
Open

[swift] Update tutorial for getRootAs -> getCheckedRoot #8409

peeeeter opened this issue Sep 27, 2024 · 6 comments

Comments

@peeeeter
Copy link

WRT #7738, the tutorial still gives

// create a ByteBuffer(:) from an [UInt8] or Data()
let buf = // Get your data
 
// Get an accessor to the root object inside the buffer.
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))

@mustiikhalil
Copy link
Collaborator

Hey,

Thanks for opening this issue! If you want to try to update the docs, feel free to open a PR for this.

@oxygen
Copy link

oxygen commented Jan 9, 2025

Sorry to bother you on an unrelated bug,

but flatc generates getRootAs* methods in Swift for you?

It doesn't for me (C# and Java are generated).

Is there something I am missing or not doing? I am doing root_type SomeTable;

@mustiikhalil
Copy link
Collaborator

mustiikhalil commented Jan 9, 2025

@oxygen so the generated getRootAs* methods are deprecated within swift. the new way is described below. Or you can follow the DocC documentation within xcode

let monster: Monster = try getCheckedRoot(.....)

let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)

@oxygen
Copy link

oxygen commented Jan 9, 2025

@mustiikhalil Thank you, this was not intuitive and I was about to compute the offset which would have been ugly. Hopefully the getRootAs functions will be undeprecated and modified into throwing from this checked thing (perhaps on the next major version).

This would save you from updating the tutorial, so we’re now relevant to this thread :)

@mustiikhalil
Copy link
Collaborator

Maybe we can add a helper function through the generator, but it will run through this function.

@oxygen
Copy link

oxygen commented Jan 9, 2025

@mustiikhalil I didn't look yet but I supose getCheckedRoot will throw instead of crashing the program with a fatal error on malformed data. So yeah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants