suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Objective-C 如何调用enum做参数的方法?

代码如下,ENUM已经定义在Account.h文件里面了

typedef NS_ENUM(NSUInteger, httpMethod){
    GET = 0,
    POST,
    PUT,
    DELETE
};
//方法定义
-(void) location:(httpMethod)httpMethod withLocation:(NSString *)location;

//在别的类里,调用方法,我想这么做,但是不知道正确的语法
[self.myAccount Account.httpMethod.PUT location: withLocation:location];
过去多啦不再A梦过去多啦不再A梦2757 Tage vor621

Antworte allen(1)Ich werde antworten

  • 天蓬老师

    天蓬老师2017-05-02 09:25:03

    你这么定义只能

    [self.myAccount location:GET withLocation:location];

    推荐把enum定义成如下:

    typedef NS_ENUM(NSUInteger, HttpMethod) {
        HttpMethodGet,
        HttpMethodPost,
        HttpMethodPut,
        HttpMethodDelete,
    }
    
    [self.myAccount location:HttpMethodGet withLocation:location];

    swift是可以做到你的要求的。

    enum HttpMethod {
        case GET
        case POST
    }
    
    func a(m : HttpMethod, b : String) {
        // ...
    }
    
    a(.GET, "hello world")

    Antwort
    0
  • StornierenAntwort