Defining Classes & its Scope

Hi


Class scope with swift and Xcode now became little confusing for me, for classes we dont use the words private and purblic when

we define them anymore so my question is how to predict its scope, for example the ViewController file we add is it public for all the

application ? and if I define a class inside it will it be public by default to all the application ?


What if we define a class in its own file ? will it be public ? and is it better to define a class in its own file or inside a view controller ?


--

Kindest Regards

Answered by Claude31 in 394679022

If you declare a class as private (or fileprivate), it will not be visible outside of itself (or its file).

Make the test:

- declare the FirstViewController as

class FirstViewController: NSViewController { }

Then in a SecondViewController class, try to create an instance of a FirstViewController

class SecondViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        let firstVC = FirstViewController()
    }


This setup works.


Now, change by adding fileprivate

fileprivate class FirstViewController: NSViewController { }


You get a compile error:Use of unresolved identifier 'FirstViewController'

It is no more visible ouside of its file.


If you declare a property in a class as private (or fileprivate), you will not be able to access to it outside of the class (or outside of the file).

That is a good practice to declare most properties as private.

A good way: declare all properties as private.

Then, if you get a compile error of undeclared propertyX, remove the private attribute.

In addition, if you open the interface file (click in the button with the 4 small square at the top left of the panel with the ViewController code and select 'Counterpart' > file (interface)) : propertyX will not show anymore.


What if we define a class in its own file ? will it be public ?

If you don't add private or fileprivate, yes it will be public. And usually, that is what you have to do.


and is it better to define a class in its own file or inside a view controller ?

If the class is specific to the VC, it can be declared inside. But it is not a good practice to put classes (which are often data models) inside the VC. It is better to get them out.

They can be in the same file, but for better readibility, it is usually better to put them in a separate file.

Just be cautious to give very explicit names to files and classes, for making their use easier later on.

Hope that's clear.

Accepted Answer

If you declare a class as private (or fileprivate), it will not be visible outside of itself (or its file).

Make the test:

- declare the FirstViewController as

class FirstViewController: NSViewController { }

Then in a SecondViewController class, try to create an instance of a FirstViewController

class SecondViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        let firstVC = FirstViewController()
    }


This setup works.


Now, change by adding fileprivate

fileprivate class FirstViewController: NSViewController { }


You get a compile error:Use of unresolved identifier 'FirstViewController'

It is no more visible ouside of its file.


If you declare a property in a class as private (or fileprivate), you will not be able to access to it outside of the class (or outside of the file).

That is a good practice to declare most properties as private.

A good way: declare all properties as private.

Then, if you get a compile error of undeclared propertyX, remove the private attribute.

In addition, if you open the interface file (click in the button with the 4 small square at the top left of the panel with the ViewController code and select 'Counterpart' > file (interface)) : propertyX will not show anymore.


What if we define a class in its own file ? will it be public ?

If you don't add private or fileprivate, yes it will be public. And usually, that is what you have to do.


and is it better to define a class in its own file or inside a view controller ?

If the class is specific to the VC, it can be declared inside. But it is not a good practice to put classes (which are often data models) inside the VC. It is better to get them out.

They can be in the same file, but for better readibility, it is usually better to put them in a separate file.

Just be cautious to give very explicit names to files and classes, for making their use easier later on.

Hope that's clear.

Thanks allot for the detailed replay, so best way to put a class in a separate file is to add file and select swift file ? and when I add functions for

a VC will it be a class methid or property as they call it some times ?

best way to put a class in a separate file is to add file and select swift file ?

Yes

Here is an example of a file I named Profiler.swift:


import Foundation

class Profiler {
   
    private var seuil               : Double    


and when I add functions for a VC will it be a class methid or property as they call it some times ?


function is a method

var is a propert

So it will be a methid for the view controller I can refer to it as VC.Method ?

If VC is your class, you will refer to VC.Method if Method is a class function.


Consider you have define a class as

class FirstViewController: NSViewController {


Inside, let us define 2 functions:


class FirstViewController: NSViewController {

    class func classTest() {
        print("Class func")
    }
  
    func instanceTest() {
        print("Instance func")
    }



Now, in another controller, we can call:

class SecondViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        FirstViewController.classTest()     // Call the class func
        let first = FirstViewController()
        first.instanceTest()                // Call the instance func
    }


We get:

Class func

Instance func


On the other hand, calling:

FirstViewController.instanceTest()

first.classTest()

causes compilation errors

Instance member 'instanceTest' cannot be used on type 'FirstViewController'; did you mean to use a value of this type instead?

Static member 'classTest' cannot be used on instance of type 'FirstViewController'


So, it is important to make a difference between class methods and instance methods.

Note also:

class names start with Uppercase:

FirstViewController


instance names start with lowercase:

let first = FirstViewController()


methods and property names start with lowercase

class func classTest() {


func instanceTest() {


var someController: FirstViewController?

Thanks allot it was great help

Defining Classes & its Scope
 
 
Q