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
}
Related
I have a class which internally uses an ivar. I don't want to expose the ivar in the public interface of the class (the header) but I declare and use it in the implementation file, like so:
//--------SomeClass.h--------------
#interface SomeClass : NSObject
#end
//--------SomeClass.m--------------
#implementation SomeClass ()
{
#protected
NSMutableDictionary *_privateData;
}
#implementation SomeClass
// ...
#end
Then in a subclass of SomeClass, I try to access _privateData:
//--------SomeSubClass.m--------------
#implementation SomeSubClass
// ...
- (void)someMethod {
NSLog(#"%#", _privateData); // NOPE
NSLog(#"%#", self->_privateData); // NOPE
NSLog(#"%#", super->_privateData); // NOPE
}
// ...
#end
But I can't. Is there a way to do this?
In order to achieve the desired behavior, you should create a subclass header file which declares all of your protected data and #import it in your subclass' .m file.
MammalSubclass.h:
#interface Mammal () {
#protected
NSMutableDictionary *_privateData;
}
//...
#end
Human.m:
#import "Human.h"
#import "MammalSubclass.h"
#implementation Human //subclasses Mammal
- (void)someMethod {
NSLog(#"%#", _privateData);
}
//...
#end
Example:
#class MyRootObject;
#interface MyObject : MyRootObject
#end
Getting form XCode:
Class MyObject defined without specifying a base class.
MyRootObject class is:
#interface MyRootObject : NSObject
- (id)init;
#end
#implementation MyRootObject
- (id)init
{
self = [super init];
if(self){
// some code here
}
return self;
}
#end
You can't inherit from a forward-declared class. You need to #include the appropriate header instead.
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.
I can't figure out why class B can access class A private instance variable.
Here is my code
A.h
#import <Foundation/Foundation.h>
#interface A : NSObject
{
#private
int x;
}
#property int x;
-(void)printX;
#end
A.m
#import "A.h"
#implementation A
#synthesize x;
-(void)printX
{
NSLog(#"%i", x);
}
#end
B.h
#import "A.h"
#interface B : A
{
}
#end
main.m
B *tr = [[B alloc] init];
tr.x = 10;
[tr printX];
Here I can access instance variable of A class x despite it is declarated as private ?
You are not accessing the private variable there, at least not directly: you are accessing a public property, which has legitimate access to the private ivar.
Your code is equivalent to this:
B *tr = [[B alloc] init];
[tr setX:10];
[tr printX];
The #synthesize statement created the getter and the setter methods for you. If you want only a getter to be available, mark your property readonly, and do all writings through an ivar in the A class.
In your implementation file do this on the top..
#interface A : NSObject
{
#private
int x;
}
#property int x;
#end
this way x will be private since it is in the implementation file. not the interface section...all classes import the interface section of A ..so it's variable are accessible to its subclasses.
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;