IOS Proxy Template - Swift

I need to create a proxy template in iOS using quick

I tried using Objective-C and here is the code

MyProtocol.h

#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@required
-(void)testMessage;    
@end

TestBO.h

#import <Foundation/Foundation.h>
#import "MyProtocol.h"

@interface TestBO : NSObject <MyProtocol>

@end

TestBO.m

#import "TestBO.h"

@implementation TestBO 

-(void)testMessage{
    NSLog(@"Test Message");
}

@end

TestProxyHandler.h

#import <Foundation/Foundation.h>

@interface TestProxyHandler : NSProxy

@property (nonatomic, strong) id object;

- (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz;

- (void)forwardInvocation:(NSInvocation *)invocation;

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;

@end

TestProxyHandler.m

#import "TestProxyHandler.h"
#import "TestBO.h"

@implementation TestProxyHandler 

- (instancetype)initWithProtocol:(Protocol *)protocol andObject:(Class)clazz{
    if ([clazz conformsToProtocol:@protocol(MyProtocol)]) {
        self.object = [[clazz alloc] init];
    }else{
        NSLog(@"Error it does not conform to protocol");
    }
    return self;
}

- (void)forwardInvocation:(NSInvocation *)invocation{
    NSString *selString = NSStringFromSelector(invocation.selector);
    NSLog(@"Called %@",selString);
    [invocation invokeWithTarget:self.object];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
    return [self.object methodSignatureForSelector:selector];
}


@end

I called him using

id <MyProtocol> delegate = (TestBO *)[[TestProxyHandler alloc] initWithProtocol:@protocol(MyProtocol) andObject:[TestBO class]];

[delegate testMessage];

But I can't get it to work in Swift, even the start line shows that the message

Testhandler.swift

import Foundation
class TestHandler: NSProxy {
    var object: AnyObject

    convenience override init(`protocol`: Protocol, andObject clazz: AnyClass) {
        if clazz.conformsToProtocol() {
            self.object = clazz()
        }
        else {
            NSLog("Error it does not conform to protocol")
        }
    }        
}

Does anyone have a clue to do this fast?

EDIT:

In java, you can create an implementation of the execution method by calling Proxy.newProxyInstance , but can this be achieved in iOS? using fast? Any clue?

+4
source share
1 answer

Objective C Swift, Swift . , , : (

NSProxy swift, super.init , C

,

,

@objc protocol SomeProt {
    // Some method
}

@objc, , , @objc c,

public func someMethod(`protocol` : Protocol, implementation : AnyClass) {
    let isImplemented : Bool = implementation.conformsToProtocol(`protocol`)
        // some code
}

- , NSCopying,

NSStringFromProtocol

NSProtocolFromString

c

ObjcHelper.h

#import <Foundation/Foundation.h>

@interface ObjcHelper : NSObject
+(NSObject *)objectForClass:(Class)clazz;
@end

ObjcHelper.m

#import "ObjcHelper.h"

@implementation ObjcHelper
+ (NSObject *)objectForClass:(Class)clazz{
    return [[clazz alloc] init];
}
@end

,

let prot : SomeProt = ObjcHelper.objectForClass(NSClassFromString("PROT_HANDLER_CLASS_NAME")) as! SomeProt

, - , , .

+2

Source: https://habr.com/ru/post/1621800/


All Articles