Objective-C: where and how should I declare enums? - objective-c

Good day, friends.
I'm newbie in Objective-C. I'm wanting to use enum in my class and make it public.
I've understand how to declare enums (http://stackoverflow.com/questions/1662183/using-enum-in-objective-c), but I don't understand where should I declare them.
I've tried:
#interface MyFirstClass : NSObject {
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
}
or:
#interface MyFirstClass : NSObject {
#public
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
}
But compiler throws error: "expected specifier-qualifier-list before typedef".
What's wrong?

.h
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
#interface MyFirstClass : NSObject {
MyTypes type;
}
.m file
type=VALUE_A;

Outside of the #interface declaration.
typedef enum myTypes {VALUE_A, VALUE_B, VALUE_C} MyTypes;
#interface MyFirstClass : NSObject {
}
#end

You can create a header file (*.h) and do following to match your enum variable.
// EnumConstants.h
#ifndef EnumConstants_h
#define EnumConstants_h
typedef enum {
VEHICLE,
USERNAME
} EDIT_TYPE;
typedef enum {
HIGH_FLOW,
STANDARD_FLOW
} FLOW_TYPE;
#endif
Uses:
#import "EnumConstants.h"
UISwitch *onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(self.tableview.frame.size.width-75, 26, 0, 0)];
onOffSwitch.tag =STANDARD_FLOW;

Related

NS_ENUM in Swift class as a property

I've created an Objective-C enum in EnumsHeader.h file and imported that into <Module>-Bridging-Header.h #import "EnumsHeader.h". I'm able to create a property of this enum type in a Swift class (ClassA.Swift). When I refer to this property from ClassB.m (Objective-C) class, I'm seeing compilation error: Property 'optionsFromA' not found on object of type 'ClassA *'
Am I missing anything here ?
EnumsHeader.h
#ifndef EnumsHeader_h
#define EnumsHeader_h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, EnumOption) {
EnumOptionA,
EnumOptionB,
EnumOptionC
};
#endif
ClassA.swift
public class ClassA: NSObject {
public var optionsFromA: EnumOption!
}
ClassB.m
#import <Foundation/Foundation.h>
#import "TestEnumsInterop-Swift.h"
#class ClassB;
#interface ClassB:NSObject
#end
#implementation ClassB
- (instancetype)init {
if (self = [super init]) {
ClassA *a = [ClassA new];
a.optionsFromA = EnumOptionA; //<--- Property 'optionsFromA' not found on object of type 'ClassA *'
}
return self;
}
#end
Objective-C has no Optional Value,
You can declare the optionsFromA as:
public class ClassA: NSObject {
public var optionsFromA: EnumOption = .A
}

How to do class constants in Objective-C?

Is there a way to use something like
if (carRecord.status == CarRecord.statusRepaired) { // using a class constant
// ...
}
such as in a car repair shop, the carRecord object's state status is checked against the CarRecord class's constant. In Objective-C, is there such a way?
You would typically do this with an enum. For example:
//=== CarRecord.h:
typedef enum CarRecordStatus {
CarRecordStatusBroken = 0,
CarRecordStatusRepaired
} CarRecordStatus;
#interface CarRecord (NSObject) {
CarRecordStatus _status;
}
#property (nonatomic, assign) CarRecordStatus status;
#end
//=== CarRecord.m:
#implementation CarRecord
#synthesize status=_status;
- (void)someMethod {
if (self.status == CarRecordStatusRepaired) {
//...
}
}
#end
Here is how would you define it in .h file :
typedef enum CarRecordStatus {
CarRecordStatusBroken = 0,
CarRecordStatusRepaired,
} CarRecordStatus;
#interface MyClassName : NSObject
..interfacebody..
#end
Use it inside MyClassName or any other just import it that's it.

expected specifier-qualifier-list before 'extern'

I'm trying to create a class with global constants:
//Resources.h
#import <Foundation/Foundation.h>
#interface Resources : NSObject
{
extern NSString * const MY_CONST;
}
#end
and
//Resources.m
#import "Resources.h"
#implementation Resources
NSString * const MY_CONST = #"my constant";
#end
And getting this nasty error: expected specifier-qualifier-list before 'extern'
What do I need to do?
Thank you
put
extern NSString * const MY_CONST;
outside of class interface declaration. MY_CONST is not a class member, so why put it inside?

cocoa Expected specifier-qualifier-list before struct

I read the other posted solutions to using structs and resolving the "Expected specifier-qualifier-list before struct" related errors, but those aren't working. Is it different in Objective C? Do I need to declare my struct somewhere else in the class? It gives me the error on the line where I declare the typedef. Here is how it looks right now:
#interface ClassA : NSObject {
NSString *name;
typedef struct _point {
uint32_t x;
uint64_t y;
} Point;
Point a;
}
#end
Put it outside of the interface:
typedef struct _point {
uint32_t x;
uint64_t y;
} Point;
#interface ClassA : NSObject {
NSString *name;
Point a;
}
#end

Objective-C. I have typedef float DuglaType[3]. How do I declare the property for this?

I have:
typedef float DuglaType[3];
#interface Foo : NSObject {
DuglaType _duglaType;
}
How do I correctly declare the property?
I tried:
// .h
#property DuglaType duglaType;
// .m
#synthesize duglaType = _duglaType;
But this spews errors.
What is the secret handshake for C++ typedefs to play nice with Obj-C properties? Thanks.
Cheers,
Doug
My work around is to bail on using #property and implement setter/getters:
// .h
(float *) duglaType;
(void) setDuglaType: (DuglaType)input;
// .m
(float *) duglaType {
return &_duglaType[0];
}
(void) setDuglaType: (DuglaType)input {
m3dCopyMatrix44f(_duglaType, input);
}
I guess typdef-ing an array give Obj-C headaches. No worries.
Cheers,
Doug
Typedef the array into a struct:
typedef struct
{
float p[3];
} DuglaType;
- (DuglaType) duglaType
{
return _duglaType;
}
- (void) setDuglaType:(DuglaType) input
{
m3dCopyMatrix44f(&_duglaType.p[0], &input.p[0]);
}