简体   繁体   中英

Passing method names as objective-c arguments?

UPDATE: I know my solution does not work. I am searching for an alternative way to solve this problem :)

code: public gist

I'm trying to create an API class that builds up some commonly used methods for restkit so I can reuse the same code to map objects and POST, GET, or PUT to my web API. When the API class is instantiated, I call responseDescriptorWithMapping, and pass a series of arguments, as you see below.

This was working fine until I tried to pass a method name as an argument.

For example:

_api = [[snapApi alloc] initWithconfigureRestKit:[Profile class] objectMapping:@{ @"_id": @"_id", @"headline": @"headline" } httpMethod:@"RKRequestMethodPOST"];

I see this warning and subsequent failure in runtime:

Incompatible pointer to integer conversion sending '__strong id' to parameter of type 'RKRequestMethod' (aka 'enum RKRequestMethod')

The restkit method is defined to take an object of RKRequestMethod class. Is it possible to use objective-c to pass method names (in the form of a string) as arguments to functions? If not how can I dynamically do this?

(instancetype)responseDescriptorWithMapping:(RKMapping *)mapping
                                   method:(RKRequestMethod)method
                              pathPattern:(NSString *)pathPattern
                                  keyPath:(NSString *)keyPath
                              statusCodes:(NSIndexSet *)statusCodes

Thanks in advance.

RKRequestMethod is defined as a bitmask not a class so you can't really pass a string as the method parameter, (ie `@"RKRequestMethodPOST"' will not work)

typedef NS_OPTIONS(NSInteger, RKRequestMethod) {
    RKRequestMethodGET          = 1 << 0,
    RKRequestMethodPOST         = 1 << 1,
    RKRequestMethodPUT          = 1 << 2,
    RKRequestMethodDELETE       = 1 << 3,
    RKRequestMethodHEAD         = 1 << 4,
    RKRequestMethodPATCH        = 1 << 5,
    RKRequestMethodOPTIONS      = 1 << 6,
    RKRequestMethodAny          = (RKRequestMethodGET |
                                   RKRequestMethodPOST |
                                   RKRequestMethodPUT |
                                   RKRequestMethodDELETE |
                                   RKRequestMethodHEAD |
                                   RKRequestMethodPATCH |
                                   RKRequestMethodOPTIONS)
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM