Skip to content

Instantly share code, notes, and snippets.

View amichnia's full-sized avatar
🎉
Swiftymocky 3.5.0 is released!

Andrzej Michnia amichnia

🎉
Swiftymocky 3.5.0 is released!
View GitHub Profile
@amichnia
amichnia / Debug+VersionInfo.swift
Last active February 9, 2019 15:27
Debug UIWindow with version label above EVERYTHING
import UIKit
class Debug {
static var window: UIWindow?
static func setupWindowIfNeeded() {
guard Debug.window == nil else { return }
guard Environment.current != .release else { return }
let window = UIWindow(frame: UIScreen.main.bounds)
// MyPrivateClass.h
@interface MyPrivateClass: NSObject
- (void) doSomethingInternalWithSecretAttribute:(NSInteger)attribute;
@end
// Somewhere in the wild west
import MyFramework
...
// This should work
let publicClass = MyPublicClass()
publicClass.doSomethingWithPrivateClass()
...
// MyPublicClass.swift
import MyFramework.Private // Required to see "private" header
public class MyPublicClass {
private let privateClass: MyPrivateClass
...
// Somewhere in the wild west
import MyFramework
import MyFramework.Private
...
// This should not be possible, nor even compile,
// as it should not be able to see MyPrivateClass
let privateClass = MyPrivateClass()
privateClass.doSomethingInternal(withSecretAttribute: 13)
// ObjectiveCToSwift.swift
@objc(MyPrivateClassProtocol) // Under this name this will cross Swift->ObjC boundary
internal protocol MyPrivateClass {
init()
func doSomethingInternal(withSecretAttribute: Int)
}
// SwiftToObjectiveC.h
#ifndef SwiftToObjectiveC_h
#define SwiftToObjectiveC_h
SWIFT_PROTOCOL_NAMED("MyPrivateClassProtocol")
@protocol MyPrivateClassProtocol
- (nonnull instancetype)init;
- (void)doSomethingInternalWithSecretAttribute:(NSInteger)attribute;
// MyPrivateClass.h
#import <Foundation/Foundation.h>
#import <SwiftToObjectiveC.h>
@interface MyPrivateClass : NSObject<MyPrivateClassProtocol>
- (void) doSomethingInternalWithSecretAttribute:(NSInteger)attribute;
@end
protocol SomeProtocol {
init()
}
class SomeClass: SomeProtocol {
...
}
// This will work
let instance: SomeProtocol = SomeClass.init()
...
@objc(Factory)
internal class Factory: NSObject {
private static var privateClassType: MyPrivateClass.Type!
@objc static func registerPrivateClassType(type: MyPrivateClass.Type) {
print("REGISTRATION CALLED WITH TYPE = \(type)")
privateClassType = type
}