Re: Initializing Structures
Re: Initializing Structures
- Subject: Re: Initializing Structures
- From: Greg Parker <email@hidden>
- Date: Wed, 05 Nov 2014 14:11:26 -0800
> On Nov 5, 2014, at 2:03 PM, Richard Charles <email@hidden> wrote:
>
> A fast and compact way to initialize a structure is to enclose the values in a pair of braces like this.
>
> NSRect rect = {{0,0},{80,20}};
> NSView *view = [[NSView alloc] initWithFrame:rect];
>
> However a compiler error occurs if we try to initialize a struct with braces directly within an Objective-C message.
>
> NSView *view = [[NSView alloc] initWithFrame:{{0,0},{80,20}}];
>
> Why does the compiler accept the first but not the second?
The C language allows a struct variable to be initialized in its declaration using the brace syntax, but does not allow that form to be used anywhere else.
C99 adds this syntax ("compound literal") that works anywhere:
NSView *view = [[NSView alloc] initWithFrame:(NSRect){{0,0},{80,20}}];
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden