Access class in another file

I'm still a beginner and I can't get a clear answer to a couple of things.

So far I've just used one file in playgrounds. If I want to use more files, how can I access the data (variables and functions) from the classes created there in my main file that controls the view?

From what I understand, having multiple files is just for convenience, so I can't write it again.

(Also on the side) what does it mean when a function is private, public or just "func"?

I use fast 3 playgrounds

thank

+3


source to share


1 answer


Making things public

will make them importable from other modules. Creation private

will only make it accessible by methods within its content area (encapsulation). For code that lives at the top level, this area is the entire file .swift

that it lives in. Without an access modifier (bare " func

" only) your thing will default to internal

, which means it is accessible from any other code inside the same module, but not via code in another module.

A special case is a modifier fileprivate

that restricts access to the file .swift

in which the code is located. For code that doesn't live in a class or struct, this does the same thing as private

. Some Swift designers discourage this modifier and may be removed in future versions of Swift.

Swift has a fifth access modifier open

, which does the same as it does public

, except that it also allows for subclassing and only applies to classes. This is rarely used, but useful for certain library interfaces.

To import all symbols public

in a module use



import Module

      

To import the single symbol public

use

import var Module.variable
import func Module.function
import struct Module.structure
import class Module.class
...

      

+2


source







All Articles