What does 'fileprivate' keyword means in Swift? [duplicate] - swift

This question already has answers here:
What is a good example to differentiate between fileprivate and private in Swift3
(10 answers)
Closed 6 years ago.
I'm starting in swift and opening a project created using swift2 from xcode 8 beta, the private modifier were changed to fileprivate. what does this keyword means? and how is different from private ?

fileprivate is one of the new Swift 3 access modifiers that replaces private in its meaning. fileprivate defines an entity (class, extension, property, ...) as private to everybody outside the source file it is declared in, but accessible to all entities in that source file.
private restricts the entity in the direct enclosing scope.

Related

What is final in Swift [duplicate]

This question already has answers here:
swift difference between final var and non-final var | final let and non-final let
(2 answers)
What is the difference between final Class and Class?
(7 answers)
Closed 3 years ago.
A newbie question.
I'm reading swift documentation AccessControl. It says there are 5 access specifiers.
Open
Public
Internal
File-private
Private
I thought Final is also an access specifier. If not what is it? And can someone give link to the documentation?
It's not an Access Control Specifier, it's a Declaration Modifier.
final
Apply this modifier to a class or to a property, method, or subscript member of a class. It’s applied to a class to indicate that the class can’t be subclassed. It’s applied to a property, method, or subscript of a class to indicate that a class member can’t be overridden in any subclass.
Source: Swift Language Reference - Declaration – Declaration Modifiers

Why extensions cannot add stored properties [duplicate]

This question already has an answer here:
Extension may not contain stored property but why is static allowed
(1 answer)
Closed 5 years ago.
Quoting the Swift Programming Language Guide :
Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.
Why is that ? What's the technical or logical reason behind this ?
In simple words
Because properties need storage, adding properties would change the memory structure of the class

Why private is unaccessible to the extension?

Here is my ViewController.swift file:
class ViewController: UIViewController {
private var myInt = 10
}
extension ViewController {
func printMyInt() {
print(myInt)
}
}
Although as mentioned in the Swift documentation - Access Levels section:
Private access restricts the use of an entity to the enclosing
declaration, and to extensions of that declaration that are in the
same file. Use private access to hide the implementation details of a
specific piece of functionality when those details are used only
within a single declaration.
Since Swift 4 has been released, I assume that I am able to implement such a thing (it is also mentioned in: What's New in Swift - WWDC 2017 session), however, the complier shows me:
'myInt' is inaccessible due to 'private' protection level
Is it incompatible with what mentioned in the documentation?
As a simple quick solution, I could declare it as:
fileprivate var myInt = 10
but I wonder why is it behaves like this, am I misunderstand what mentioned in the documentation? or is it a "Xcode" bug (used 9.0.1 version)?
Remark: The project has been created in the older Xcode 8 and then migrated to Xcode 9.
In Swift 4, private members are accessible to extensions of that declaration that are in the same file, see SE-0169 – Improve Interaction Between private Declarations and Extensions.
If the project has been created in Xcode 8 (with Swift 3) then
Xcode 9 will open it in "Swift 3 mode" and set the "Swift Language Version" to "Swift 3.2". Therefore the stricter Swift 3 restrictions hold.
To make the private extension visible to extension in the same file,
set the Swift language version to 4 in the build settings. (Of course
that might make more changes in your code necessary.)
Even better, use "Edit -> Convert -> To Current Swift Syntax ..."

When to make a class as Singleton in Swift? [duplicate]

This question already has answers here:
Difference between static function and singleton class in swift [closed]
(3 answers)
Closed 6 years ago.
I have made a class that will contain all the utility methods. So, instead of making it as a singleton, I have marked the methods as static and accessing those methods by the ClassName.methodName without the need for instantiation.
Is this approach OK?
Just consider that a singleton is used in order to ensure that only one instance exists for a given class, and that there’s a global access point to that instance.
I believe that having all utility functions marked as static within a class is a good approach since, as you have stated, you will need to use ClassName.methodName in order to use them.
In addition, based on what you want to achieve and the information provided by this link, I would reassert that having a class with static methods is the best alternative.

Static and non-static methods in scala [duplicate]

This question already has answers here:
What is the rationale behind having companion objects in Scala?
(7 answers)
Closed 7 years ago.
As we know we don't have static methods in scala. If we have to achieve that taste we declare that class as object. But the problem is that when we declare class as object then all methods present in that object becomes static. And in class all methods all non-static. What I want is that to have static as well as non static methods in the same class, Is it possible? Definitely it would be possible but how??????
To do what you are trying do in Scala you create an object and a class of same name. Put static in object and instance members in Class version of it. This object is called a companion object.