Questions tagged [designated-initializer]

In Cocoa programming, a designated initializer is the method through which all of the instance's initial parameters can be set. A designated initializer is typically executed implicitly when not called explicitly. In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index.

In Cocoa programming, a designated initializer is a method that accepts all of the instance's initial parameters and initializes the instance. This method is typically something other than - (id)init, and is usually documented as the designated initializer in the header file. A designated initializer is typically executed implicitly when not called explicitly. As an example, the designated initializer of UIViewController is - (id)initWithNibName:bundle:.


In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index. This feature was introduced with the C99 standard.

Designated initializers have two forms (ISO 9899:2011 6.7.9):

If a designator has the form
[ constant-expression ]
then the current object (defined below) shall have array type and the expression shall be an integer constant expression. If the array is of unknown size, any nonnegative value is valid.

If a designator has the form
.identifier
then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

Example of designated initializer usage (9899:2011 6.7.9/35):

struct { int a[3], b; } w[] =
       { [0].a = {1}, [1].a[0] = 2 };

In the example, both the above mentioned forms of designated initializers are combined.

135 questions
6
votes
2 answers

When do I need to call -[UIViewController initWithNibName:bundle:]?

In post Using initWithNibName changes absolutely nothing, he shows two uses of the same View Nib definition, in the first case, he simply calls alloc/init and the second, he specifies initWithNibName. So, while this always works: MyViewController…
bill.lee
  • 2,207
  • 1
  • 20
  • 26
6
votes
4 answers

How to resolve designated initialization error for UITableViewController?

I'm new to swift and I'm having problems declaring my initializer in my PlacesTableViewController class. It prompts me "Super.init isn't called before returning from initializer" and when I added the Super.init, it prompts me: "Must call a…
6
votes
1 answer

-Wmissing-field-initializer when using designated initializers

I'm using GCC 4.6.2 (Mingw) and compiling with -Wextra. I'm getting strange warnings whenever I use designated initializers. For the following code typedef struct { int x; int y; } struct1; typedef struct { int x; int y; } struct2; typedef…
Lundin
  • 195,001
  • 40
  • 254
  • 396
5
votes
1 answer

Using designated initializers for initializing a 2D char array initializer in a struct emits an error C2078 in VS2013

I'm using VS2013. The whole program is C, not C++. I can initialize an "array of strings" like this without any problems: char titles[4][80] = { "Dad", "Idiot", "Donut Lover", "Fewl" }; // OK! I have a struct declared like this: typedef struct { …
5
votes
0 answers

How to mark designated initializers of super class "invalid" in Objective-C?

From the Adapting Modern Objective-C document: If a class provides one or more designated initializers, it must implement all of the designated initializers of its superclass. That means if I have a subclass of NSObject that has its own designated…
Frank Rupprecht
  • 9,191
  • 31
  • 56
5
votes
1 answer

C struct initialization with C99 - Is mixing named and unnamed members valid?

Given the following: struct example_struct { char c; int i; }; Is any the following initializer syntax valid in C99? Syntax example #1 struct example_struct example = { 'a', .i = 1}; Syntax example #2 struct example_struct example = { .c =…
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
4
votes
2 answers

How to use designated initialization of derived aggregates in C++?

I have an aggregate structure B derived from another aggregate A. I would like to initialize it and there are two options: ordinary aggregate initialization and C++20 designated initializers: struct A {}; struct B : A { int x; }; int main() { …
Fedor
  • 17,146
  • 13
  • 40
  • 131
4
votes
1 answer

C++20: Force usage of designated initializers to emulate named function arguments

I am a big fan of using ad-hoc structs and designated initializers to emulate named parameters for my functions. struct Args { int a; int b; }; int f(Args a) { return a.a + a.b; } int g() { return f({.a = 1, .b = 2}); // Works!…
Vogelsgesang
  • 684
  • 1
  • 4
  • 15
4
votes
1 answer

Why has Clang decided to allow designated initializers in C++?

I thought that designated initializers were discontinued in C++ and only worked in C. However, I came across a simple example which compiled and worked fine with clang++. int main() { int a[6] = { [4] = 29, [2] = 15 }; } g++:…
Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
4
votes
1 answer

esoteric C designated initializer failure, compiler bug or feature?

I just spent until about 1AM tracking down a bug in my code and what I found really surprised me. The actual code is very complex involving unions of structures containing unions of structures, etc. but I have distilled the issue down to the…
4
votes
6 answers

Evaluation of a statement in C language

struct { int a[2], b; } arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2}; How to evaluate this line in C language? General declaration of struct is different from this statement. Also accessing of an element in C can be done like…
user7869838
4
votes
2 answers

Using Designated Initializers with the Heap

One can use designated initializers as shown below (for "billy") without issue, but when the same initialization approach is used on dynamic memory things will break at compile-time. What are the restrictions for using designated…
sherrellbc
  • 4,650
  • 9
  • 48
  • 77
4
votes
1 answer

Calling an instance method in designated initializer of a Swift class

In Swift programming language, it is mandatory that “A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.” source of quote from another…
4
votes
3 answers

Why must I keep declaring the same required but not implemented initializer for init(coder aDecoder) for my programatic UIViewController subclass?

Perhaps it is just me, but I find certain aspects of swift... obtuse to say the least. I don't use Interface Builder most of the time because I like using PureLayout. So I was hoping to make a UIViewController subclass, say PureViewController, that…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
4
votes
2 answers

C99 Designated Initializer duplicate index not flagged at all in build output or lint

I played around with designated initializers a bit the other day and noticed, to my surprise, that it is valid to use the same index more than once. What's more, it didn't even produce a compiler warning, error, or even informational statement when…
johnny
  • 4,024
  • 2
  • 24
  • 38
1 2
3
8 9