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
1
vote
1 answer

How to call protocol extensions initializers in designated initializers?

I'm trying to inject a protocol extension initializer into an existing class's designated initializer. I don't think there's a way around it without overriding the designated initializer from the class, then call the protocol extension initializer…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
2 answers

Check designated initializer in xcode

Is there any way to find out which initializer is the designated one in super class in Xcode? I type super().init.. then, Xcode shows all initializers of superclass. I want to know is there any sign or symbol to point out which is the designated…
Yao
  • 709
  • 2
  • 11
  • 22
1
vote
1 answer

loadNibNamed vs. initWithFrame dilemma for setting frame's height and width

I created a UIView subclass associated with .xib file. This UIView subclass is to be used in a UIViewController. In the controller, I think there are two options how we instantiate the UIView subclass: MyUIView *myView=[[MyUIView alloc]…
0
votes
1 answer

how does designated initializers work

I am having some trouble understanding designated initializers. I am studying Objective C from the book "Learn Objective C on the Mac". The following is an implementation file. #import "Tire.h" @implementation Tire - (id) init { if (self =…
Rounak
  • 613
  • 3
  • 8
  • 22
0
votes
1 answer

Preserve indentation formatting when using C++20 designated initializers (VS2019)

Is there a way to get Visual Studio 2019 to preserve existing indentation when writing typing . to start typing the next field? MyStruct a = { .asd = 12, .foo = 1, } While typing the following code, as soon as I type . for the foo field with…
0
votes
1 answer

How to indent in vim for designated initializers?

How to do proper indentation in vim for designated initializers of structs (in C)? This is how it is indented by default: typedef struct { int foo; int bar; } child_t; typedef struct { child_t child; } parent_t; int main(int argc, char…
Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39
0
votes
1 answer

How to designated initialize a C++ struct that has a construcotr?

I have a very large struct that has customized copying constructor, customized moving constructor, customized moving assignator, and customized copying assignator, but I also need to use Designated Initialization syntax somewhere to initialize it,…
0
votes
0 answers

Fastest way to transform C multilevel designated initializer list to C++?

I have a designated initializer list with multiple levels from C-Code like the following and I want to transform the whole codebase to C++. C-Code: wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_ESP_WIFI_SSID, .password =…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
0 answers

Designated Initializer won't work with char array

I am working on an IoT project and using ESP32 as a microcontroller for my devices. This simple struct in c++ stores the basic state of a device: struct DeviceState { char UID[12]; DeviceMode mode; DeviceErrorType errorCode; char…
zak
  • 115
  • 1
  • 9
0
votes
1 answer

How can I reference a parameter within a designated initializer?

I want to add the value of a parameter to another parameter within a designated initializer, without having to define a separate variable to hold that value. Is this possible? typedef struct { int x; int y; } point; int main() { point p = { …
Dio
  • 35
  • 3
0
votes
3 answers

Why can't you initialize a protocol that has default implementation?

I've read the In swift, why can't I instantiate a protocol when it has an initialiser? My question is focused on why the compiler can't look into your default implementation and initialize an object based on that? protocol ViewModel { var…
mfaani
  • 33,269
  • 19
  • 164
  • 293
0
votes
2 answers

Const struct initialization with custom elements in C++

I am porting some C code to C++ and I am trying to initialize a struct with some values. I want the struct to be stored in flash (const) and not in RAM, and its values are typedef'd elements. Originally, I had it like this: typedef struct { …
0
votes
0 answers

Ambiguous reference to member 'init(coder:)' when calling super.init(nibName: nibNameOrNil, bundle: nil)

While trying to implement the cocoa pod, BWWalkthrough, into my current project programmatically, I ran into a problem with the init functions/inheritance. I tried to subclass BWWalkThroughViewController in my own class…
0
votes
1 answer

Objective-C: minimal init versus init that calls initWith?

The method init in most classes does a minimal initialization, because if you need more complexity, more has-a related behaviors, then you need to use an initWith method. So why would anybody call an initWith from within the init method? I have seen…
JDD
  • 1
  • 2
0
votes
0 answers

C++ struct public member construction using named a desigator list?

The following code compiles fine in g++. #include #include using namespace std; struct thing { int a; char b; string name; }; int main() { thing t = { a : 23, b : 'e', name : "Hello" …
magnus
  • 4,031
  • 7
  • 26
  • 48
1 2 3
8
9