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
169
votes
4 answers

What does dot (.) mean in a struct initializer?

static struct fuse_oprations hello_oper = { .getattr = hello_getattr, .readdir = hello_readdir, .open = hello_open, .read = hello_read, }; I don't understand this C syntax well. I can't even search because I don't know the syntax's…
Benjamin
  • 10,085
  • 19
  • 80
  • 130
72
votes
2 answers

Swift - Must call a designated initializer of the superclass SKSpriteNode error

This code worked on first XCode 6 Beta, but on latest Beta it's not working and gives such errors Must call a designated initializer of the superclass SKSpriteNode: import SpriteKit class Creature: SKSpriteNode { var isAlive:Bool = false { …
Kosmetika
  • 20,774
  • 37
  • 108
  • 172
67
votes
8 answers

How to write init methods of a UIViewController in Swift

I am unable to add init method to the following UIViewController class. I need to write some code in the init method. Do i have to write init(coder) method? Even when I add the coder and decoder methods I still get errors. I also tried using the…
Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
39
votes
2 answers

Designated initializers in C++20

I've got a question about one of the c++20 feature, designated initializers (more info about this feature here) #include constexpr unsigned DEFAULT_SALARY {10000}; struct Person { std::string name{}; std::string surname{}; …
MateuszGierczak
  • 398
  • 1
  • 4
  • 10
37
votes
6 answers

C++ Equivalent to Designated Initializers?

Recently I've been working on some embedded devices, where we have some structs and unions that need to be initialized at compile time so that we can keep certain things in flash or ROM that don't need to be modified, and save a little flash or SRAM…
James Snyder
  • 3,992
  • 2
  • 20
  • 16
36
votes
3 answers

What is a designated initializer in C?

I have an assignment that requires me to understand what are designated initializers in C, and what it means to initialize a variable with one. I am not familiar with the term and couldn't find any conclusive definitions. What is a designated…
user7349461
  • 387
  • 1
  • 3
  • 5
30
votes
2 answers

Why C++20 doesn't support out-of-order designated initializer?

While I was reading C++ reference, I had a question about this paragraph: Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization…
Sadeq
  • 7,795
  • 4
  • 34
  • 45
21
votes
1 answer

What happens to fields not named by a designated initializer?

Since C99 (and C++20), it's possible to initialize structs using this syntax: struct info { char name[8+1]; int sz; int typ; }; struct info arr[] = { [0] = { .sz = 20, .name = "abc" }, [9] = { .sz = -1, .name = ""…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
18
votes
5 answers

Strange values while initializing array using designated initializers

When I initialize the array below all the output looks ok except for values[3]. For some reason values[3] initialized as values[0]+values[5] is outputting a very large number. My guess is that I am trying to assign values[0]+values[5] before they…
17
votes
2 answers

Compound literal and designated initializer warning from GCC but not Clang

Compiling with gcc -std=c99 -Wextra this piece of code: #include struct T { int a; int *b; int c; }; int main(void) { struct T t = {.b = ((int []){1, 1})}; printf("%d\n", t.b[1]); return 0; } Is giving me a…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
17
votes
1 answer

What's the difference between a required initializer and a designated initializer?

I was creating my own custom tableViewCell and then I got an error saying: 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell' I looked it up and obviously it's a must to implement that as well. But this led to…
mfaani
  • 33,269
  • 19
  • 164
  • 293
17
votes
2 answers

Turn off designated initializer checking in Xcode 6

I'm getting the compile error: error: convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers] Compile-checked designated initializers might be a good thing, but if I don't want deal with that…
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
16
votes
5 answers

Which initializer(s) to override for UITableViewController subclass

I have a UITableViewController subclass that's instantiated, depending on where it's used, in a NIB or via code. In both cases I want to do customization in the initializer method. Does that mean I need to implement both initWithNibName:bundle:…
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
16
votes
1 answer

Adding NSCoding as an Extension

I'd like to extend a framework class (I don't want to edit the source code directly), and make it conform to NSCoding. Basically, here's a simplification of the situation I'm in : /* Can't be edited. */ class Car: NSObject { var color:…
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
15
votes
6 answers

Why are designated initializers not implemented in g++

Is there any specific reason why has support for designated initializers not been added to g++? Is the reason that C99 standards came late and g++ was developed earlier and later people didn't care about this issue, or there is some inherent…
Bharat
  • 2,139
  • 2
  • 16
  • 35
1
2 3
8 9