Objective-C 如何调用enum做参数的方法?
过去多啦不再A梦
过去多啦不再A梦 2017-05-02 09:23:03
0
1
620

代码如下,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梦

Antworte allen(1)
Peter_Zhu

你这么定义只能

[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")
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage