Say I have the following function that returns a CLLocationCoordinate2D, which is just a struct that has two doubles (named longitude and latitude) in it:
- (CLLocationCoordinate2D)getCoordinateForHouse:(House *)house
{
CLLocationCoordinate2D coordToReturn;
coordToReturn.latitude = // get house's latitude somehow
coordToReturn.longitude = // get house's longitude somehow
return coordToReturn;
}
Can I basically treat this struct just like any other primitive type? For instance, if I call the function above in code somewhere else like this:
CLLocationCoordinate2D houseCoord =
[someClassThatTheAboveFunctionIsDefinedIn getCoordinatesForHouse:myHouse];
The value that was returned from the function is simply copied into houseCoord (just like any other primitive would act), right? I don’t have to worry about the CLLocationCoordinate2D ever being destroyed elsewhere?
Seems obvious to me now that this is probably the case, but I just need confirmation.
This is an area where Objective-C inherits its behaviour directly from C — assigning structs causes a shallow copy. So if you have any pointers inside the structs then you’ll copy the pointer, not the thing pointed to. In C code you need to factor that in to your adhoc rules about ownership. In Objective-C the automatic reference counting compiler isn’t capable of dealing with references to objects within structs (or, I think, unions) so it’s smart not to become too attached to the idea in any event.