Skip to content

Instantly share code, notes, and snippets.

@SoundBlaster
Last active November 30, 2023 11:43
Show Gist options
  • Save SoundBlaster/0e53bae4ab814537c5868564d95eb553 to your computer and use it in GitHub Desktop.
Save SoundBlaster/0e53bae4ab814537c5868564d95eb553 to your computer and use it in GitHub Desktop.
Base example for NSProxy in Swift
// 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)
@SoundBlaster
Copy link
Author

Don't forget about Bridging Header if you mix languages!
https://developer.apple.com/documentation/swift/importing-objective-c-into-swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment