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 / String+HTML.swift
Created November 6, 2019 13:38
Swift: Convert basic raw html string to attributed string
import UIKit
extension String {
private var attributedStringOptions: [NSAttributedString.DocumentReadingOptionKey: Any] {
return [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
}
// AdditionFrameworkInterop.m
#import "AdditionFrameworkInterop.h"
@import AdditionalSDK;
// You can adopt protocols from additional framework even if its weak linked
@interface AdditionFrameworkInterop() <PrimeNumberCheckerDelegate>
@property (strong, nonatomic) PrimeNumberChecker *primeChecker;
@end
// AdditionFrameworkInterop.h
#import <Foundation/Foundation.h>
#import <MainSDK/MainSDK-Swift.h>
@interface AdditionFrameworkInterop : NSObject
@property (weak, nonatomic) id<InteropDelegate> delegate;
+ (BOOL)additionalModuleAvailable;
// Below would import or not, depending on whether it is linked
@import AdditionalSDK;
// ...
+ (BOOL)additionalModuleAvailable {
if ([AdditonalSDK class]) { // Or any class from additional SDK
return YES;
} else {
return NO;
// MainSDK
#if canImport(AdditionalSDK)
import AdditionalSDK
public class NumberInspector: PrimeNumberCheckerDelegate {
// ... adopt delegate protocol from AdditionalSDK
}
#else
public class NumberInspector {
// ... cannot adopt protocol from AdditionalSDK
// AdditionalSDK
@objc public class PrimeNumberChecker: NSObject {
@objc public var delegate: PrimeNumberCheckerDelegate?
@objc public func inspect(number: Int) {
// ...
}
}
@objc public protocol PrimeNumberCheckerDelegate {
// MainSDK
public class NumberInspector {
public weak var delegate: NumberInspectorDelegate?
// ...
public func inspect(number: Int) {
// perform inspection and notify delegate when done
}
}
// MyPrivateClass.m
#import "MyPrivateClass.h"
@implementation MyPrivateClass
@synthesize privateProperty;
+ (void)load {
// This is called once, when module is being loaded,
// "Invoked whenever a class or category is added to the Objective-C
...
@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
}
protocol SomeProtocol {
init()
}
class SomeClass: SomeProtocol {
...
}
// This will work
let instance: SomeProtocol = SomeClass.init()