Class initialisation

Hi, e.g in java we make instances from classes by "new" keyword. and we had a main class that was ran automatically.
The swift in iOS apps from where is start? and classes in where initialize?
e.g in storyboard I create a table view controller and so I create a new class that inherited from UITableViewController.
and I connect my tableViewController to my class.
but where it is initialised?
and if I in another class make a new one from that class what happen?
are that the same? or the cells will be change? or what?
please help me?

What is problem in your code ?


but where it is initialised?


What do you want to know exactly ?

Class properties are initialized in viewDidLoad.

It starts by initializing from its super class and then its own properties :


    override func viewDidLoad() {
       
        super.viewDidLoad()
       // Then, initialize class's properties



If you sub class this class, it will inherit all properties, unless you overload them.

My question is about swift programming in iOS.
I have problem with class communication.
So I found delegate and protocol for that.
Ok that works.
Now I want know that where are instances?
I said for example in java or C we have a main class that will be run as default class.
If we want to make instances from our class we use "new" keyword in code and have access to our instances.
If you don't understand what I want to know exactly tell me to explain more.
thanks 🙏

Accepted Answer

I said for example in java or C we have a main class that will be run as default class.

Swift’s startup process is somewhat different from both Java and C, although it’s most similar to C.

By default a Swift program starts by executing code in a file called

main.swift
. If you create a Swift command-line tool target (File > New Project > macOS > Command Line Tool) you’ll see exactly that. A GUI application can include a custom
main.swift
that starts the GUI framework. For example, on iOS it would call
UIApplicationMain
. This code is responsible for instantiating the
UIApplication
singleton, instantiating the app delegate, connecting them up and starting the app.

As a convenience Swift allows you to avoid the

main.swift
by adding the
@UIApplicationMain
attribute to your app delegate. Once you do this Swift synthesise
main.swift
to contain the standard boilerplate code that calls
UIApplicationMain
. You can learn more about this in in the Attributes section of The Swift Programming Language.

However, the above just covers the basic startup process. Once the GUI framework is up and running, instantiation becomes more complex. Let’s consider one case you mentioned earlier:

e.g in storyboard I create a table view controller and so I create a new class that inherited from UITableViewController. and I connect my tableViewController to my class. but where it is initialised?

If you’re talking about the main storyboard (typically

Main.storyboard
), that is referenced by the
UIMainStoryboardFile
property in your app’s
Info.plist
. When
UIApplication
starts up it looks for that property and, if present, opens that storyboard. It then finds the initial view controller in that storyboard and instantiates it. The storyboard (well, the nib within the storyboard) knows the name of class of that view controller, so it instantiates that class by name and then initialises it by calling the
init?(coder:)
from the
NSCoding
protocol.

It’s possible to override various parts of this machinery, but most folks don’t need to do that. Instead they focus on plugging in to the machinery at one of the standard override points, like the

viewDidLoad
method that Claude31 mentioned.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

I have a TableViewController object in Main.storyboard and in custom class I assigned the myTable.swift class to TableViewController and myTable.swift inherited from UITableViewController.

And I have ViewController object in Main.storyboard and in custom class I assigned the myView.swift class to ViewController and myView.swift inherited from UIViewController.

I want to add cells by clicking on a button in the ViewController. e.g save button.

For store that I considered an array in myTable.swift.

How can handle this problem?

The delegate and protocol are good for this(?) but any other solution is there?

Thank for your response.

The array in myTableView.swift is the dataStore of the tableView ?


So, all you have to do when you update the array is to call for a dataReload of the tableView.


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { }

that will handle the update gently


If you don't want to delegate:

- in the tableViewController, addObserver for a notification "kUpdate"

- associate an action which is simply

tableView.reloadData()

- in the view (where the save button is), post a kUpdate "notification"


Note: in the naming of your classes, don't forget to start with Uppercase, as MyView.

🙏 a lot

Class initialisation
 
 
Q