Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting dot syntax to bracket syntax on a struct

Tags:

objective-c

I have a property of CGSize:

@property (nonatomic) CGSize descriptionSize;

'

@synthesize descriptionSize = _descriptionSize;

I can access the height through the dot syntax:

self.descriptionSize.height = 35;

but how does this work with the bracket syntax?

[self setDescriptionSize:???];

Looked stupid simple to me, but I can't get the clue. Thanks in advance!

like image 358
brainray Avatar asked Dec 12 '22 02:12

brainray


1 Answers

This is one of the pitfalls of dot notation for properties: Those two dots in self.descriptionSize.height look the same but mean very different things. The first is a property accessor which maps to a "get descriptionSize" method, but the second is an old-school struct reference. The first dot returns a CGSize scalar, NOT a pointer to the size value in the object. When the second dot sets the height in that returned CGSize, it's setting a value on the stack instead of changing the value in the object. This is how you have to do it:

CGSize size = self.descriptionSize;
size.height = 35;
self.descriptionSize = size;

…or the equivalent without properties dot notation:

CGSize size = [self descriptionSize];
size.height = 35; // we still use the dot here: size is a struct not an object
[self setDescriptionSize:size];
like image 52
davehayden Avatar answered Feb 13 '23 14:02

davehayden