How can I get an iPhone application to dynamically load protocols?

I am writing an RPC implementation with proxy objects (I think NSDistantObject). In this case, we generate header files (both DTO: s and protocols) directly from the server implementation. I dynamically create objects at runtime, but I ran into a specific problem.

When you come across an object, I have a name in the form of a string, and from there I will figure out what to do with it. My problem arises when I try to β€œrequest” a system for a protocol that is not used anywhere in the code. The file header is, even #import: ed.

Some examples ...

This works great:

Protocol *protocol = NSProtocolFromString(@"UIApplicationDelegate"); // Returns a protocol

If I take one of my own protocols and indicate that his delegate should match him, he also works without problems:

@interface ApplicationDelegate : NSObject <UIApplicationDelegate, ACMyCustomProtocol> {

}

// Implementation...

Protocol *protocol = NSProtocolFromString(@"ACMyCustomProtocol"); // Returns a protocol

, , nil:

Protocol *protocol = NSProtocolFromString(@"ACMyCustomProtocolNotMentionedAnywhere"); // Returns nil

(, Xcode ) . Protocol *objc_getProtocol(const char *name) .

Edit:

, , , , , .

Objective-C :

, , ( ) arent, .

+3
1

#import ed, @protocol(MyProtocolName) - , , Protocol . , - NSDictionary, Protocol. NSProtocolFromString , , . - :

+ (Protocol)remoteProtocolForName:(NSString *)name
{
    static NSDictionary *dict = nil;
    if (!dict)
    {
        dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                @protocol(Foo), @"Foo",
                ...];
    }
    return [dict objectForKey:name];
}

, , Protocol NSDictionary...

+4

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


All Articles