I know that such questions are asked before. I looked on the net find examples but I can not figure out how to use NS_ENUM
in my code. I have a list of error codes. To manage them I want to use NS_ENUM
. I create a class which contains all my public const for the application. I want to define my enum here and use it everywhere in the app.
My code in .h file:
typedef NS_ENUM(NSInteger, wsErrEnum);
In my .m file:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
I thought that I can check in the following manner:
if(result.ErrCode == wsErrEnum.ERR_NONE) {
// do stuff ...
}
, but ERR_NONE
is not visible as property.
So my question is how to define and use NS_ENUM
in this case.
EDIT:
Using
if(result.ErrCode == ERR_NONE) {
NSLog(@"It is OK!");
}
give me error that ERR_NONE
is undefined
.
EDIT 2:
I understand the problem. I declare NS_ENUM
in my .h
file (as some comments and answers suggest) and the error disappears. I try this before but in that case I did not use enum properly - I use is as wsErrEnum.ERR_NONE
. 10x for help.
PS My class is #imported
in the .m file where I try to make this comparison.
The only problem here is that you've declared the enum's values in your .m file.
You should declare the entire enum (values included) in your .h file. By declaring the enum type wsErrNum
in your .h file and the values in your .m file, you've hidden the values from all other classes.
So, in your .h file:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
Nothing needs to go in your .m file to declare this enum.
In Swift :
if result.ErrCode == wsErrEnum.ERR_NONE {
// do stuff ...
}
In Objective-C :
if(result.ErrCode == ERR_NONE) {
// do stuff ...
}
The real issue is that you have split up the declaration of wsErrEnum
, remove it from the .m file and place the full delcaration in the .h:
typedef NS_ENUM(NSInteger, wsErrEnum) {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 1
};
But better is something like:
typedef NS_ENUM(NSInteger, wsErrEnum) {
wsErrEnumName = 0,
wsErrEnumTimeOut = 1
};
Just a small remark, properties like your ErrCode
should not started with a capital letter.
Normally, I use Enumerations in objective-c as typedef enum
. Doing like this, you don't need an implementation file (.m) and the code looks like this:
// Enumerations.h
@interface Enumerations
typedef enum:int {
ERR_NONE,
ERR_HOST_TIMEOUT
} wsErrEnum;
@end
By default, the compiler values the enumerations from 0 to N, in order of which they are declared. In the example above, ERR_NONE
is 0
and ERR_HOST_TIMEOUT
is 1
.
But you could also value the enumerations the way you preffer:
typedef enum:int {
ERR_NONE = 0,
ERR_HOST_TIMEOUT = 504
} wsErrEnum;
Then, you could work with switch
and it would consider your enumeration, so it throws a warning in case you don't implement one possible result, for example:
switch(result.ErrCode) {
case ERR_NONE:
//implementation
break;
}
The code above will warn you that you don't have a case implemented for ERR_HOST_TIMEOUT
.
You can also import your Enumerations.h
in the file YourProjectName-Prefix.phc
so it can be used in your hole project.
//
// Prefix header
//
// The contents of this file are implicitly included at the beginning of every source file.
//
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "Enumerations.h" //Add this line so you don't have to import your .h file every time you use it
#endif
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.