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

浏览:45日期:2023-12-02

问题描述

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

问题解答

回答1:

你这么定义只能

[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')

相关文章: