check if a particular property in a class (not an instance of class) exist - iphone

i have to create a class method and i need to check if a particular string correspond to a property in a class.
example:
#interface aClass
#property (nonatomic, strong) aType *property1;
#end
in other class:
+ (X*)createXwithProperty:(NSString*) aProperty
{
if([aClass haveProperty:aProperty])
{
return create(X);
}
else return nil;
}
How can i achieve this ?

Since it is a #propery it has getter and setter method.
In your example:
#property (nonatomic, strong) aType *property1;
Getter method should be called property1 as your property, so you can check if your class instance responds to that selector like this:
Convert NSString to selector:
SEL selector = NSSelectorFromString(#"property1");
if ([aClassInstance respondsToSelector:selector]) {
NSLog(#"RESPONDS");
}
Sorry, I was to fast on answering. I see now that you want to check if class has specific property, but you don't have a class instance.
To see if class has specific property use this (basically, the principle it is the same as with class instances :) ):
SEL selector = NSSelectorFromString(#"property1");
if ([aClassNotInstance instancesRespondToSelector:selector]) {
NSLog(#"RESPONDS");
}

Related

Using objective C++, can you create a property that is a C++ class and the getter returns a reference

I want to do this but have not seen it anywhere.
#property (nonatomic) MyCPPClass &myInstance;
I need it to write a getter that returns a reference and not a copy.
- (MyCPPClass &)myInstance {
return _myInstance;
}
Or can it only be done with an iVar and writing my own getter and setter.
You may use a private instance variable for that:
#interface MyObjCPPClass
#property (nonatomic) MyCPPClass &myInstance;
#end
#implementation MyObjCPPClass {
MyCPPClass _myInstance;
}
- (MyCPPClass &)myInstance {
return _myInstance;
}
- (void)setMyInstance:(MyCPPClass &)instance {
_myInstance = instance;
}
#end

How to use delegates in ios 5

I need to use a delegate object in an iOS application. I have declared the delegate as this:
In the class where the function is defined:
#interface OOObjectCommandInterface : NSObject<OOCameraControllerDelegate>
In the class where the function must be invoqued:
(In de .h file)
#protocol OOCameraControllerDelegate
- (void)drawFrame:(CVImageBufferRef) imageBuffer:(BOOL)flip;
#end
and
#interface OOCameraController : UIViewController
{
...
id<OOCameraControllerDelegate> delegate;
}
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
Aditionally, where the second class is initialized:
_hardwareController.delegate = [OOObjectCommandInterface ocInterface];
where _hardwareController is an instance of OOCameraController class.
So, when I try to invoque the delegate object, I do this:
[delegate drawFrame:imageBuffer:flip];
but the function is not executed. Any idea?
P.D.: The function I am calling is a singleton class. Could be any problem there?
Have you set delegate to self in the second class? Create an object in the second class like
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
and then [_hardwareController setDelegate:self];
By definition, a singleton is a design patron to access an object, unique, that only can be created 1 time (first get_instance you do). With get_instance, you can access from everywhere, to the functions inside the singleton, so, Why you are not using it directly?
Write something like [[MySingletonClass get_instance] FunctionThatIWantToUse:...]; And don't use a delegate

Type of property 'shapeMatched' does not match type of accessor 'setShapeMatched:'

I'm developing an iPhone 4 application and I having problems with the class:
#interface Pattern : NSObject {
int maxNumShapes;
NSMutableArray* shapes;
NSMutableArray* shapeMatched;
CGSize bounds;
}
#property (nonatomic, retain, readonly) NSMutableArray* shapes;
#property (nonatomic, retain, readonly) NSMutableArray* shapeMatched;
...
- (void) setShapeMatched:(ShapeType)type;
#end
And its implementation:
- (void) setShapeMatched:(ShapeType)type {
int index = 0;
if (shapes != nil)
{
for(Object2D* obj in shapes)
{
if (obj.figure == type)
{
[shapeMatched replaceObjectAtIndex:index
withObject:[NSNumber numberWithBool:YES]];
obj.withFillColor = YES;
break;
}
else
index++;
}
}
}
I get the following warning:
Type of property 'shapeMatched' does not match type of accessor 'setShapeMatched:'
How can I fix this warning?
You method - (void)setShapeMatched:(ShapeType)type is probably intended to change something in the shapeMatched array, however, its name overwrites the setter of your property for that very same array.
It's unclear, but if you really want to overwrite the setter, the argument must be of type NSMutableArray * since that's the type of your property. Otherwise, if you method just do something else, rename it to avoid the collision. setShapeMatchedType:(ShapeType)t might be a good option.
You need to rename your custom setShapeMatched: to something else (like setMyShapeMatched). setShapeMatched: is already in use as the setter for your declared property.

How to override #synthesized getters?

how to override a property synthesized getter?
Just implement the method manually, for example:
- (BOOL)myBoolProperty
{
// do something else
...
return myBoolProperty;
}
The compiler will then not generate a getter method.
Inside of your property definition you can specify getter and setter methods as follows:
#property (nonatomic, retain, getter = getterMethodName, setter = setterMethodName) NSString *someString;
You can specify the getter only, the setter only, or both.
Just implement your own getter and the compiler will not generate one. The same goes for setter.
For example:
#property float value;
is equivalent to:
- (float)value;
- (void)setValue:(float)newValue;
I just want to add, I was not able to override BOOL property with getter/setter, until I add this :
#synthesize myBoolProperty = _myBoolProperty;
so the complete code is :
in header file :
#property BOOL myBoolProperty;
in implementation file :
#synthesize myBoolProperty = _myBoolProperty;
-(void)setMyBoolProperty:(BOOL) myBoolPropertyNewValue
{
_myBoolProperty = myBoolPropertyNewValue;
}
-(BOOL) myBoolProperty
{
return _myBoolProperty;
}

Can protocols within protocols be treated as inclusive of the protocol they adopt?

I am assigning protocols in a couple classes that follow an inheritance tree. Like so:
first class
#protocol LevelOne
- (void) functionA
#end
#interface BaseClass : NSObject <LevelOne> {
}
second class
#protocol LevelTwo <LevelOne>
- (void) functionB
#end
#interface SubClass : BaseClass <LevelTwo> {
}
Later I am assigning the class as delegate properties of other classes
base class
#interface AppClass : NSObject {
#protected
id<LevelOne> levelOneDelegate;
}
#property (assign) id<LevelOne> levelOneDelegate;
subclass
#interface AppClassesFriend : AppClass {
#protected
id<LevelTwo> levelTwoDelegate;
}
#property (assign) id<LevelTwo> levelTwoDelegate;
At the end of this journey, AppClassesFriend has 2 properties on it.
"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.
However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).
In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.
Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".
So, what I would like to be able to do is :
SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;
so inside AppClassesFriend (a) I could use :
[self.levelTwoDelegate functionA]; <---- this is never found
[self.levelTwoDelegate functionB];
but it seems I have to add
a.levelOneDelegate = s;
Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?
Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):
#interface AppClassesFriend : AppClass {
#protected
id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}
#property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;