我在Objective-C程序中遇到enum可见性问题.我有两个头文件,一个定义了一个typedef enum.另一个文件需要使用typedef'd类型.
在直接C中,我只是#include其他头文件,但在Objective-C中,建议不要#import在头文件之间使用,而是@class根据需要使用前向声明.但是,我无法弄清楚如何转发声明枚举类型.
我不需要实际的枚举值,除了相应的.m实现文件,我可以安全地#import离开.那么如何才能typedef enum在标题中识别?
在Objective-C中使用特定类型创建枚举的正确方法是什么?NS_ENUM和NS_OPTIONS如何工作?NS_OPTIONS用于掩码,如NSAutoresizing?谢谢.
Code from NSObjCRuntime.h
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#define NS_OPTIONS(_type, _name) _type _name; enum : _type
Run Code Online (Sandbox Code Playgroud) 这是在Objective-C中使用emum的正确(甚至是有效的方法)吗?即,未使用menuItem,只是定义了一个列表add = 1,load = 2,list = 3等.
enum menuItems {
add = 1 ,
save ,
load ,
list ,
removeAll ,
remove ,
quit
};
int optionSelect;
scanf("%d", &optionSelect);
switch (optionSelect) {
case add:
//...
break;
}
Run Code Online (Sandbox Code Playgroud)
欢呼加里
我想知道可可的不同风格的枚举声明背后的理由是什么?
像这样:
enum { constants.. }; typedef NSUInteger sometype;
Run Code Online (Sandbox Code Playgroud)
是否有理由使用typedef来获取NSUInteger的分配而不进行强制转换?
有时typedef是NSInteger/NSUInteger,为什么不总是使用NSInteger?使用NSUInteger真的有好处吗?
枚举标记名有时仍然使用,就像这里的_NSByteOrder.
这个答案也非常有用:Objective-C中的typedef枚举是什么?.
我试图使用NSLog来打印控制台消息.问题是有时我在调用时收到"EXC_BAD_ACCESS"错误
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"Working test %d", toInterfaceOrientation);
NSLog(@"EXC_BAD_ACCESS %@", toInterfaceOrientation);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我只想看看传递给函数的参数包含什么.第一个NSLog工作正常.第二个导致"EXC_BAD_ACCESS",我不明白为什么?
我正在将我的iOS应用程序转换为64位.我安装了最新的Xcode 5.1(beta 4).
当我编译应用程序时,我收到了超过100个警告,其中大部分很容易修复.但是,我对以下代码发出警告:
+ (CommentResponseStatus)commentReponseStatusCodeWithStatusString:(NSString *)_status
{
NSArray *commentStatusString = [NSArray arrayWithObjects:@"success", @"needConfirmation", @"stopped", nil];
return [commentStatusString indexOfObject:_status];
}
Run Code Online (Sandbox Code Playgroud)
在哪里CommentResponseStatus声明:
typedef enum {
success,
needConfirmation,
stopped
} CommentResponseStatus;
Run Code Online (Sandbox Code Playgroud)
我有一个警告"隐式转换失去整数精度:' NSUInteger'(又名' unsigned long')到' CommentResponseStatus'"
警告就行了 return [commentStatusString indexOfObject:_status];
在NSArray我们有- (NSUInteger)indexOfObject:(id)anObject;
我对此警告感到困惑,现在不知道如何修复它.任何快速帮助将不胜感激.
我有一些UIPopoverViewControllers消息传递委托UIViewControllers传递UI事件.我没有为每个事件编写单独的方法,而是在委托中有一个方法,它有一个switch语句,它根据传递的常量确定如何处理事件(例如下面的例子).
这可能是糟糕的设计,但这是我想出来的.我已经看到关于枚举或静态类的这个问题,但是不理解答案.
So..is我在做什么坏,是有办法,我可以在一个地方定义枚举,这样我就不必维护多个代码位,可以很容易地得到不同步?
编辑好的,多挖一点(这里 + 这里)我看到我可能在正确的轨道上.所以我想我需要知道implementationiOS中文件的位置和位置.
enum {
kSetPlaybackType = 0,
kSetAllNotesOn,
kSetAllNotesOff,
kSetVelocity,
kSetDuration
};
- (void)barPropertyAction:(int)action withParam:(NSNumber *)param
{
switch (action) {
case kSetPlaybackType:
playbackType = [param intValue];
if (playbackType == kPalindrome){
palindromeDirection = kPalindromeUp;
}
break;
case kSetAllNotesOn:
for (BarNote* note in self.barNoteArray) {
note.noteOn = YES;
}
[self.bar updateWindows];
break;
case kSetAllNotesOff:
for (BarNote* note in self.barNoteArray) {
note.noteOn = NO;
}
[self.bar …Run Code Online (Sandbox Code Playgroud) 如果不是,它需要被包含在一个单独的文件(如MyEnums.h),我需要在每次.M或.h文件中要参考的类型或值的一个时间#IMPORT MyEnums.h?
这是MyClass.h的示例代码:
#import <Foundation/Foundation.h>
// #1 placeholder
@interface MyClass : NSObject {
}
// #2 placeholder
- (void)sampleMethod:(MyEnum)useOfEnum;
// #3 placeholder
@end
Run Code Online (Sandbox Code Playgroud)
这是我想要定义的枚举:
typedef enum MyEnum {
Value1,
Value2
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试将我的枚举定义放在#1中,我会收到错误:在'interface'之前没有指定类型或存储类.
如果我尝试将我的枚举定义放在#2中,我会收到错误:期望的标识符或'('在'结尾'之前.
如果我尝试将我的枚举定义放在#3中,我会在'MyEnum'之前得到错误:expect')'.哪个抱怨参数"useOfEnum",因为它的类型尚未定义.
那可以这样做吗?或者,最好的方法是什么,并限制#imports所需的数量?
我正在查看NSString头文件,看看Apple如何编写枚举并遇到这段代码:
enum {
NSStringEncodingConversionAllowLossy = 1,
NSStringEncodingConversionExternalRepresentation = 2
};
typedef NSUInteger NSStringEncodingConversionOptions;
Run Code Online (Sandbox Code Playgroud)
这给我留下了几个问题.
typedef NSUInteger NSStringEncodingConversionOptions;行是否是正常包含的好主意,还是仅在此使用,因为它们声明了匿名枚举?如何为变量分配枚举并在以后访问其值?我认为这很简单,但每次我尝试将enum值赋给变量(Xcode中没有类型不匹配或警告出现)时,我的应用程序崩溃并出现EXC_BAD_ACCESS错误.
以下是我enum在头文件(BarTypes.h)中设置的方法:
typedef enum {
BarStyleGlossy,
BarStyleMatte,
BarStyleFlat
} BarDisplayStyle;
Run Code Online (Sandbox Code Playgroud)
没有问题(至少阅读和使用这些值).但是,当我创建一个可以存储其中一个enum值的变量(BarStyleGlossy,BarStyleMatte或BarStyleFlat)然后尝试设置该变量时,应用程序崩溃了.以下是我设置和使用变量的方法:
//Header
@property (nonatomic, assign, readwrite) BarDisplayStyle barViewDisplayStyle; //I've also tried just using (nonatomic) and I've also tried (nonatomic, assign)
//Implementation
@synthesize barViewDisplayStyle;
- (void)setupBarStyle:(BarDisplayStyle)displayStyle {
//This is where it crashes:
self.barViewDisplayStyle = displayStyle;
}
Run Code Online (Sandbox Code Playgroud)
为什么会在这里崩溃?如何将枚举的值存储在变量中?我认为这个问题与enums我最终缺乏理解有关,但如果我遵循传统的变量设置和分配等,这应该有效.关于我做错了什么的任何想法?
请注意我是enums的新手,所以我的词汇可能有些混乱(原谅我 - 如果你知道我想说的话,可以自由编辑).
我发现了一些关于enums网络的参考文献:
objective-c ×9
enums ×8
ios ×4
cocoa ×2
typedef ×2
64-bit ×1
c ×1
extern ×1
header ×1
header-files ×1
iphone ×1
nsinteger ×1
nsuinteger ×1