Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 118 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hide adoption of protocol in swift

#1
I was wondering if I could do a hidden adoption of a protocol in swift.

In Objective-C, I would use a private header in the m.-file to hide protocols that I don't want to be exposed to the outside.
Is something like that possible in swift? Putting a "private" before the protocol does not work, apparently :)
Reply

#2
Maybe the language has changed since the post, but it works for me. This is an example of where I wanted to hide an initializer to control the lifetime of an object and perform post processing. In this case, I wanted to track and send analytics, based on the caller's configuration.

private protocol Reportable {
init()
var people:[String:AnyObject] { get }
var track:[String:AnyObject] { get }
}


public class Analytics {

public final class Alpha: Reportable {

var thingOne: String?
var thingTwo: String?

private init() {}
private var people:[String:AnyObject] { return [:] }
private var track:[String:AnyObject] { return [:] }
}

public final class Bravo: Reportable {

var thingOne: String?
var thingTwo: String?

private init() {}
private var people:[String:AnyObject] { return [:] }
private var track:[String:AnyObject] { return [:] }
}


public static func alpha(configure:Alpha -> ()) {
return report(configure)
}

public static func bravo(configure:Bravo -> ()) {
return report(configure)
}


private static func report<T:Reportable>(configure:T -> ()) {
let event = T()
configure(event)
Analytics.doSomething()
}


static func doSomething() {

}
}

// separate file
func clientCode() {

Analytics.alpha { event in

event.track // error

event.thingOne = "foo"
event.thingTwo = "bar" }

Analytics.bravo { event in
event.thingOne = "foo"
event.thingTwo = "bar" }

}
Reply

#3
**Edit:** Found a simple way to hide it. At least from the xcode quick help information: Just put the adoption and implementation in an extension of you class and it will not show up there.

**Original Answer:**

I came up with this example, inspired by inner classes in Java. Here `MyVC` does not expose that it implements `UICollectionViewDelegate`for internal purposes, while the delegate implementation has access to `MyVC`'s private variables.


public class MyVC: UIViewController {

private let a = 4 as Int
private var hiddenDelegate: HiddenDelegateImpl?

public override func viewDidLoad() {
super.viewDidLoad()
hiddenDelegate = HiddenDelegateImpl(outer: self)
innerCollectionView.delegate = hiddenDelegate
}
}

private class HiddenDelegateImpl: NSObject, UICollectionViewDelegate {

private weak var outer: MyVC?

init(outer: MyVC) {
self.outer = outer
super.init()
}

private func doStuff() -> Int {
// can access private variables of outer class
return outer?.a
}

// implement delegate methods here
}

Note that `HiddenDelegateImpl` could also be an inner class of MyVC, I chose to put it outside for readability.

In contrast to Java instances of inner classes need an instance of the outer class to exists. Since this is not the case with Swift we need to have the `outer` workaround.

There is also [this nice example][1] which focuses on delegate implementation.

**Edit:** Made the delegate an instance variable in the outer class to retain it and the reference to the outer class weak to avoid retain cycles.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through