Last active
November 30, 2023 11:43
-
-
Save SoundBlaster/0e53bae4ab814537c5868564d95eb553 to your computer and use it in GitHub Desktop.
Base example for NSProxy in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Objective-C Header BaseProxyForSwift.h | |
NS_ASSUME_NONNULL_BEGIN | |
NS_SWIFT_NAME(BaseProxyForSwift) | |
@interface BaseProxyForSwift : NSProxy | |
+ (id)with:(id)object; | |
@end | |
NS_ASSUME_NONNULL_END | |
// Objective-C Implementation BaseProxyForSwift.m | |
#import "BaseProxyForSwift.h" | |
@interface BaseProxyForSwift () | |
@property (nonatomic, strong) id object; | |
@end | |
@implementation BaseProxyForSwift | |
+ (id)with:(id)object { | |
BaseProxyForSwift *res = [self alloc]; | |
res.object = object; | |
return res; | |
} | |
@end | |
// Swift Implementation BaseSwiftProxy.swift | |
import Foundation | |
public class BaseSwiftProxy: BaseProxyForSwift { | |
} | |
// Usage in Swift | |
let a = NSObject() | |
let p = BaseSwiftProxy.with(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget about Bridging Header if you mix languages!
https://developer.apple.com/documentation/swift/importing-objective-c-into-swift