Questions tagged [initializer]

Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters

684 questions
49
votes
4 answers

static readonly field initializer vs static constructor initialization

Below are two different ways to initialize static readonly fields. Is there a difference between the two approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = …
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
43
votes
5 answers

Initializer does not override a designated initializer from its superclass

So I've just upgraded to Xcode 6.3 Beta 3 and a lot of error(s) are appearing relating to the following: Initializer does not override a designated initializer from its superclass. override init() { super.init() } For example this is a…
gotnull
  • 26,454
  • 22
  • 137
  • 203
39
votes
5 answers

CoreData: error: Failed to call designated initializer on NSManagedObject class

I have a little damn problem with CoreData. I want to insert a new Object, so I first have to create one. This is done by that code: Challenges *newChallenge = [[Challenges alloc] init]; [newChallenge setName:@"TestChallenge"]; [newChallenge…
Bene
  • 636
  • 1
  • 8
  • 24
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
36
votes
1 answer

How to satisfy a protocol which includes an initializer?

I defined a simple class: class MyClass { var name:String? required init() { println("init") } } I can add a new initializer in an extension like this: extension MyClass { convenience init(name: String) { …
Klaas
  • 22,394
  • 11
  • 96
  • 107
33
votes
7 answers

Is it possible to use a c# object initializer with a factory method?

I have a class with a static factory method on it. I want to call the factory to retrieve an instance of the class, and then do additional initialization, preferablly via c# object initializer syntax : MyClass instance = MyClass.FactoryCreate() { …
Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
33
votes
5 answers

Subclassing NSWindowController in Swift and init(windowNibName)

I am trying to start a new document based Cocoa project in Swift and want to create a subclass of NSWindowController (as recommended in Apple's guides on document based apps). In ObjC you would make an instance of an NSWindowController subclass…
Martin
  • 333
  • 1
  • 3
  • 6
32
votes
4 answers

What Is a Curly-Brace Enclosed List If Not an intializer_list?

I asked a question here: Lifetime Extension of a initializer_list return involving the non-functional code: const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; }; I believed the lambda was trying to return an…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
32
votes
2 answers

Does Rails run initializers for rake task?

Are the scripts from config/initializers executed when I run rake task?
Paul
  • 25,812
  • 38
  • 124
  • 247
30
votes
2 answers

need self to set all constants of a swift class in init

I have a Swift class that has a constant ivar (are they called instance constants now?). To set the value to this constant, I need to call an initializer of the desired object and pass itself. However, I am not allowed to as I need to initialize all…
Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
27
votes
1 answer

Why can in-class initializers only use = or {}?

In-class initializers (C++11 feature) must be enclosed in curly braces or follow a = sign. They may not be specified inside parenthesis. What is the reason for this?
7cows
  • 4,974
  • 6
  • 25
  • 30
26
votes
4 answers

When global_variables_initializer() is actually required

import tensorflow as tf x = tf.constant(35, name='x') y = tf.Variable(x + 5, name='y') # model = tf.global_variables_initializer() with tf.Session() as session: print("x = ", session.run(x)) # session.run(model) print("y =…
Vinay
  • 327
  • 1
  • 3
  • 9
25
votes
5 answers

Can I declare variables of different types in the initialization of a for loop?

Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the…
grzkv
  • 2,599
  • 3
  • 26
  • 37
22
votes
3 answers

Reduce function with three parameters

How does reduce function work in python3 with three parameters instead of two. So, for two, tup = (1,2,3) reduce(lambda x, y: x+y, tup) I get this one. This would just sum up all the elements in tup. However, if you give reduce function three…
chanpkr
  • 895
  • 2
  • 10
  • 21
22
votes
3 answers

How to check if database schema matches Entity Framework schema?

For my surprise, using the CreateDatabaseIfNotExists context initializer, the line context.Database.Initialize(true) doesn't throw an exception if the schema does not match my code first schema. Is there a way to validate if the current database…
1
2
3
45 46