Objective-C LanguageStructs


Syntax

  • typedef struct { typeA propertyA; typeB propertyB; ... } StructName

Remarks

In Objective C, you should almost always use an object instead of a struct. However, there are still cases where using a struct is better, such as:

  • When you're going to be creating and destroying a lot of values of the (struct) type, so you need good performance and small memory usage
  • Structs are faster to create and use because when calling a method on an object, the method has to be determined at runtime
  • Structs take up less size because objects have an extra property isa, which holds their class
  • When the value has only a couple of properties and a small total size (take CGSize; it has 2 floats which are 4 bytes each, so it can take up 8 bytes), and is going to be used a lot (ties in with the first point)
  • When you could use unions or bitfields, and importantly, need the size saved by them because you need small memory usage (ties in with the first point)
  • When you really want to store an array inside of the struct, since Objective-C objects can't directly store C-arrays. However, note that you can still "indirectly" get an array in an Objective-C object by making it a reference (i.e. type * in place of the C-array type[])
  • When you need to communicate with some other code, such as a library, that's coded in C; structs are fully implemented in C but objects are not