WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

struct / class : how to tell programmatically

Forums > 100 Days of SwiftUI

Given how a copy of a class variable can change the values of the properties in the original, it seems pretty important to know whether the object you're dealing with is a struct or a class.

If you've written the code yourself or have access to the source, that might not be a problem, although it might be a lot of work. But if you have a compiled library (?) and little documentation, is there any way for a program to tell if something is a struct or a class?


Do you want to know at compile time or at run time?

At compile time, you can do this:

thing1 === thing2

and you will get a build error if thing1 and thing2 are not classes or other reference types.

I can't honestly see why you would need that info at run time.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.