Reputation: 1545
I was trying to use the objective c class extension header (only the .h) template that is new in Xcode. I dont know how to. Well i did File - > New File - > objective c class extension header and gave it a name "UICodeButton" and it inherits from UIButton. Now in that .h file i wrote @property (read write) NSInteger * cID; and just below that the implementation.So like this:
#import <UIKit/UIKit.h>
@interface UIButton ()
@property (readwrite) NSInteger * codeID;
@property (readwrite) NSInteger * pID;
@property (readwrite) NSString * cDesc;
@property (readwrite) NSString *pDesc;
@end
@implementation UIButton
@synthesize codeID;
@synthesize pID;
@synthesize cDesc;
@synthesize pDesc;
@end
i am using this in viewcontroller class..so i have (ViewCP.h and ViewCP.m) files. In the .m file i am importing #import "UIButton_UICodeButton.h" Here in this file i am writing in the view did load the following
- (void)viewDidLoad
{
NSInteger tt = (NSInteger) S_B_T;
NSLog(@" the value is %d",tt);
self.btnJobType.pID = &(tt);
}
Output: the value is 555
i have S_B_T set as 555 in the define. When it hits the self.btnJobType.pID= &(tt); It throws an error * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setPID:]: unrecognized selector sent to instance. So i am not writing something correct with it. Where should i write the set method for pID? I thought synthesize pID would do the trick. But apparently not. So how do i proceed? i searched examples for the "objective c class extension" .But couldnt find any. If more information is needed please let me know. Thanks
Upvotes: 1
Views: 1343
Reputation: 308
You could also write a category on an existing class in another use case. For some classes, creating subclasses is forbidden. Categories (like @interface SOMEClass (MyCategory)
) work, but need to implemented with great care as you are not supposed to change the inherited behavior, just add to it.
Upvotes: 0
Reputation: 104698
You are prohibited from creating class continuations (e.g. @interface SOMEClass ()
) on classes which you do not implement. Consequently, you cannot add fields/properties to framework classes. Of course, you can use categories to add either class methods or instance methods.
Instead, you would create a subclass and add your properties/ivars there.
Upvotes: 4