How can a Swift module / class work without language support for "secure" members?

I am faced with a situation where I am defining a reusable base class in a module, and I want to provide certain functions that should only be called by subclasses and not by external users of that subclass.

I am writing a framework and packaging it as a Swift module. Part of my structure includes a base class that can be subclassed to add functionality, but the derived class also has other external goals. Imagine that you are defining a new kind of representation: it comes from UIViewor NSView, then it provides additional logic and then it is instantiated by the other side.

In this case, I define the UIView-like class , which is intended for the subclass, and with it comes a lot of private UIViewinternal things, such as dimension, organization, who knows, internal material.

The fact is that the end users of this new class of representations do not want to see the interior of the architecture supporting the subclass, they must be completely inside the black box of what the subclass is.

And it seems to me that now this is not possible in Swift.

I really don't understand why Swift got rid of access control protected. According to Apple, the function that I want to show only to subclasses is "not very useful outside the subclass, so protection is not critical."

Am I missing something? Is this a whole class of design patterns that Swift simply cannot support?

One thought that comes up with me is that I could divide the public-public and private-public parts of my class into two parts, possibly using protocols in which public-public users will see only the public protocol and "private "public users will also see the" private "protocol. Alas, this seems like a lot of technology for what used to be free.

+4
source share
2 answers

You can, curiously, sort it by dividing the material for subclasses into a separate protocol, for example:

class Widget {
    protocol SubclassStuff {
        func foo()
        func bar()
        func baz()
    }

    func makeSubclassStuff() -> SubclassStuff {
        // provide some kind of defaults, or throw a fatalError if this is
        // an abstract superclass
    }

    private lazy var subclassStuff: SubclassStuff = {
        return self.makeSubclassStuff()
    }()
}

, , , .

, , . , , Objective-C -style prefixed name:

protocol WidgetConcreteTypeStuff {
    ...
}

protocol Widget {
    var concreteTypeStuff: WidgetConcreteTypeStuff { get }
}
+2

FWIW. Swift ( protected), Swift . , 3,5 , , Swift , Swift , , , .

- , 95% , . , , . , , , , , API , (, , ), API, , , .

, , , Objective C, protected, API ( ) "+ Subclassing", , .

Swift , :

open class SomeClass {
    private var foo: String
    private var bar: Data
    public init(){
        foo = "foo"
        bar = Data()
    }
    private func doInternalThing() {
        print(foo)
    }
}

( , ), ( ) :

// Create a nested "Protected" type, which can accept an instance of SomeClass (or one of its subclasses) and expose the internal / protected members on it
public extension SomeClass {
    public class Protected {
        unowned private var someClass: SomeClass
        public var foo: String {
            get {
                return someClass.foo
            }
            set {
                someClass.foo = newValue
            }
        }
        public init(_ someClass: SomeClass) {
            self.someClass = someClass
        }
        public func doInternalThing() {
            someClass.doInternalThing()
        }
    }
}

, :

class SomeSubclass: SomeClass {
    private lazy var protected: SomeClass.Protected = { SomeClass.Protected(self) }()
    func doSomething() {
        protected.foo = "newFoo"  // Accesses the protected property foo and sets a new value "newFoo"
        protected.doInternalThing() // Prints "newFoo" by calling the protected method doInternalThing which prints the foo property.
    }
}

. - , , , . , , SomeClass.Protected , , , , , , .

, , "" var, API. , - API , . SomeSubclass - API .

, , - - API - , Swift , Swift Swift, Twitter bugss.swift. .

+2

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


All Articles