UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.