Christos Hayward
Christos Hayward

Reputation: 5993

How do initializations of C structs work in iOS's Objective-C?

From https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html#//apple_ref/doc/c_ref/CGSize :

struct CGSize {
   CGFloat width;
   CGFloat height;
};
typedef struct CGSize CGSize;

CGSize is an unadorned C struct and doesn't accept messages.

What are appropriate ways to (in my case) create a CGsize with its dimensions to be 0?

If I were working straight C, it would be a use case for calloc() or whatnot. What is the Objective-C idiom for this use case?

Upvotes: 0

Views: 190

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108111

the specific case you can use

CGSize zeroSize = CGSizeZero;
CGSize zeroSize = (CGSize){ 0, 0 };
CGSize zeroSize = (CGSize){ .width = 0, .height = 0 };
CGSize zeroSize = CGSizeMake(0,0);

Keep in mind that many cases the frameworks such as Core Graphics and Core Foundation provide you with constants such as CGSizeZero that you can use, so you generally don't need to dynamically initialize such structs with calloc.

Upvotes: 4

picciano
picciano

Reputation: 22701

The easiest way to get one with 0 for both dimensions would be the constant, CGSizeZero

Another way would be CGSizeMake(0,0)

Upvotes: 2

Related Questions