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
0
votes
0 answers

super.init() error: must call a designated initializer of the superclass "x"

I created a basic custom class inheriting from an existing one as follows: As you can see, it gives me the basic error of the designated initializer of the superclass. However, I did use it! The superclass has only one init() with no arguments. How…
0
votes
1 answer

Why does Swift invoke the wrong initialiser?

I have a UINavigationController subclass: fileprivate class NavController: UINavigationController { override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) { super.init(navigationBarClass: navigationBarClass, toolbarClass:…
0
votes
1 answer

call UIView initWithFrame even when self is loaded from NIB?

I inherited a code base and found the compiler complaining about this piece of code: - (id)initWithFrame:(CGRect)frame { // is this really missing?? // self = [super initWithFrame:frame]; self = [[[NSBundle mainBundle]…
thst
  • 4,592
  • 1
  • 26
  • 40
0
votes
1 answer

Designated Initializer?

what is this saying as stated in apples doc: Sometimes the designated initializer of a superclass may be sufficient for the subclass, and so there is no need for the subclass to implement its own designated initializer. Other times, a class’s…
0
votes
1 answer

Convenience inits in class hierarchy cause infinite recursion

I have a hierarchy of 2 classes in Swift 2.0. Both classes can be instantiated by passing the arguments or by passing a JSON dictionary ([String: AnyObject]) which contains the arguments. The inits that directly take the arguments are designated…
cfischer
  • 24,452
  • 37
  • 131
  • 214
0
votes
3 answers

Objective-C: Why not call the designated initializer?

I've inherited this code: - (id)initWithLocation:(CLLocation *)inLocation { if (self = [super init]) { _location = [inLocation copy]; } return self; } - (id)initWithLocation:(CLLocation *)inLocation offsetValue:(NSNumber…
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
0
votes
0 answers

NSWindowController initialization warning

I am developing a document-based app. I am using a dedicated window controller for the document window, and calling the -[NSDocument makeWindowControllers] method. My window controller is initialized like this: - (instancetype) init { if (self =…
0
votes
0 answers

Initializing an IBOutlet with a designated initializer? (Using storyboard)

is it possible to initialize an IBOutlet of a custom UIView type with a designated initializer (using storyboard)? If yes when and how should the initialization in the UIViewController occur. Thanks in advance.
Moonstar
  • 247
  • 1
  • 4
  • 13
0
votes
2 answers

Initialization of a custom UIView in a UIViewController using a storyboard

I have a custom UIViewcontroller and wanted to initialize and assign a custom UIView which I assigned to an IBOutlet before. I'm using a storyboard. Can anybody give me hints where to call the designated initializer of the custom…
Moonstar
  • 247
  • 1
  • 4
  • 13
0
votes
0 answers

What do three dots mean in a designated initializer?

I found this in linux/include/linux/cpumask.h: #define CPU_BITS_ALL \ { \ [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \ } #endif…
user1655874
0
votes
1 answer

Calling a superclasses designated initialiser calls the subclasses

I have what seems like a straightforward enough issue, but I just have no idea why it's working the way it is. I have a class Shape which has a subclass of Square. When I call Square and call its designated initialiser, in self = [super init] it…
Mark Reid
  • 2,611
  • 3
  • 23
  • 45
0
votes
2 answers

Dedicated Initializer in Objective-C

I am newbie to Objective-C. I have a 'XYZPerson' Class with attributes {firstName, lastName, dateOfBirth} and I want when I write "XYZPerson *person=[[XYZPerson alloc] init]" in main, it should call my overridden 'init' method which should in-turn…
0
votes
2 answers

(iOS) Black screen when custom initializing UIViewController

This is my first question here. I'm trying to make an app that will work with Core Audio. I found this framework http://theamazingaudioengine.com/ that I'm trying to use and so far I managed to do the first thing in the documentation, which is to…
kanstraktar
  • 5,357
  • 2
  • 21
  • 29
0
votes
1 answer

Initializing instance object in designated initializer?

I have a Rectangle class which has properties width and height. It also has an instance property/object called origin (-(XYPoint *) origin ). Below is the code for my designated initializer in which I pass the XYPoint object as an argument. Is there…
0
votes
1 answer

Designated initializer and passing arguments

I have this hierarchy: CreateAnObjectClass : NSObject MySecondClass : MyBaseClass MyBaseClass : NSObject in CreateAnObjectClass I want to create an instance of MySecondClass method and i want to pass a @property (strong,nonatomic)…
Jakub
  • 13,712
  • 17
  • 82
  • 139
1 2 3
8
9