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
3
votes
1 answer

Swift Initializer Mystery

Few blocks of code: This works: import Foundation class Class1 { init(param1: String?) { print("hello") } convenience init() { self.init(param1: nil) } } class SubClass1: Class1 { } let obj = SubClass1() In the…
kerry
  • 2,362
  • 20
  • 33
3
votes
2 answers

Overriding superclass initializer in Swift

I've found numerous examples for using the Singleton pattern in Swift 3. I'm trying to use this method: class Records: RailsData { static let shared = Records() private init() {} ... } When I do, I get the compiler error: Overriding…
curt
  • 4,422
  • 3
  • 42
  • 61
3
votes
4 answers

why return and throw keywords are not allowed in static block

class TestCase{ static{ System.out.println("hello"); return; } public static void main(String[] args){ System.out.println("main"); } }
3
votes
3 answers

Swift 3: Convenience Initializer Extending Foundation's 'Timer' Hangs

I am attempting to extend Foundation's Timer class in Swift 3, adding a convenience initializer. But its call to the Foundation-provided initializer never returns. The problem is illustrated in the following trivialized demo, which can be run as a…
Jerry Krinock
  • 4,860
  • 33
  • 39
3
votes
1 answer

Are initializer expressions part of the constructor in D?

In D, can I initialize directly on declaration and expect the initializer expressions are part of the constructor? I came from C# and there this is the case. But with DMD 2.071.0 Im getting other behavior. class Other { } class Test { Other…
3
votes
1 answer

How to override SKScene init(fileNamed: fileNamed)

I have a SKScene class in which I need to implement a custom initializer that override the initializer init(fileNamed: fileNamed) of the SKScene superclass SKNode, in order to do some proprietary initializations but keep the possibility offered by…
Jonah Begood
  • 317
  • 2
  • 14
3
votes
1 answer

Swift: Why is the init?(length length: Int) NSMutableData's initializer failable?

The memory allocation can fail, but I think Swift doesn't handle that cases. The code on github calls a non failable initializer public convenience init?(length: Int) { let memory = malloc(length) self.init(bytes: memory, length: length,…
soyer
  • 316
  • 2
  • 10
3
votes
1 answer

Unpickling object of type X in the __new__ method of class X calls __init__ on returning the unpickled object, why?

I have an object that will be cached after it's first use. I will do this using the cPickle module. If the module is already cached, when I try to instantiate the object the next time around (in another process) I would like to use the cached…
J. Doe
  • 427
  • 5
  • 8
3
votes
1 answer

Swift Inheritance v.s calling super

I was learning the following chapter in The Swift Programming Languages: If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Then I tried these codes in my…
Evan
  • 430
  • 6
  • 16
3
votes
1 answer

Using an implicitly-typed array in class initializer

Consider the following: public class Foo { public List ListProp { get; set; } = new List(); public int[] ArrayProp { get; set; } = new int[3]; } public static void Main() { new Foo { // This works, but does not…
Matt Jenkins
  • 2,824
  • 1
  • 30
  • 34
3
votes
2 answers

Swift initializer for public class

When creating a public class, is it necessary to make the designated initializer public? What's the difference between making it public vs not? e.g. public class A { init() {} } or public class A { public init() {} }
Boon
  • 40,656
  • 60
  • 209
  • 315
3
votes
0 answers

Failable initializers for classes in Swift 2

While talking about failable initializers, The Swift Programming Language Tutorial by Apple states that unlike value types, for classes, the failable initializer check for nil should come after all stored properties are initialized to a default…
Omer
  • 39
  • 2
3
votes
1 answer

Calling an async method from the database seed method

I am coding a MVC 5 internet application and would like to know how to call an async method from the seed method when a database is created. Here is my code: public class ApplicationDbInitializer : CreateDatabaseIfNotExists { …
Simon
  • 7,991
  • 21
  • 83
  • 163
3
votes
3 answers

Inheritance and initialization in swift

I have been trying to create two different classes: for the superclass I create my properties and init() function with the appropriate parameters. However, when I create my subclass with its own properties and init() function I get into trouble.…
Zapato33
  • 53
  • 8
3
votes
2 answers

How to write an initializer so it will only run when the database is set up correctly?

I have an initializer that wants to fetch a variable from the database. This initialization is only needed when running the app with rails server or in production, as it is only used in the views. config.default_country_id =…
berkes
  • 26,996
  • 27
  • 115
  • 206