No more `private init` in Swift?

I saw several references to using private init in Swift to limit the construction of an object (like this ), but it doesn't seem to be like when I try (in Xcode 7.2.1 Playground):

 class C { private init() {} } var c = C() // No errors. 

Am I missing something or is it really a mistake?

+5
source share
1 answer

You probably expect private to limit the use of the class definition, but that is not what it does.

The definition of private is to "restrict the use of an entity to its own defining source file."

From the book Swift Access Control .

EDIT:

As with Swift 3 fileprivate does fileprivate make private and private more restrictive in the sense that it "limits the use of the object to the conclusion"

+11
source

Source: https://habr.com/ru/post/1244871/


All Articles