1

The following codes crashed:

@interface AppDelegate (PrivateMethods)

@property (nonatomic, strong) NSString * name;

@end

@implementation AppDelegate

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.name = @"foobar";
    ...

Error is:

'-[AppDelegate setName:]: unrecognized selector sent to instance 0x6d73df0'

When I change

@interface AppDelegate (PrivateMethods)

to

@interface AppDelegate ()

Then it is okay, what would be the reason?

Update: As answered below, since I must use class extension for this purpose, now this question become: Is the use of class extension to declare private methods acceptable?

e.g.

@interface AppDelegate ()

- (void) start;
@property (nonatomic, strong) NSString * name;

@end
Ryan
  • 10,041
  • 27
  • 91
  • 156

2 Answers2

2

Class extension is basically used to enhance the public property variable. Suppose you have exposed readonly object, or getter method of any variable, then you have make the same object as readwrite in extension.Whereas Category is only used to enhance the method/functionality of class.

check this

@interface MyClass : NSObject
// property here is used as readonly.
@property (retain, readonly) float value;
@end

// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end

or

@interface MyClass : NSObject
// here we have exposed getter method of private instance.
- (float) value;
@end

// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, strong) float value;
@end
Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
0

One is a category, the other is a class extension. If you want to add properties to an existing class, you need to use the latter.

This is the right approach:

@interface AppDelegate ()

@property (nonatomic, strong) NSString * name;

@end
J2theC
  • 4,412
  • 1
  • 12
  • 14
  • Thanks, usually I will use category to implement private methods (not visible in .h), so now can I put the private methods inside extension instead of category? Is it a common practice? e.g. @interface AppDelegate () - (void) start; \@property (nonatomic, strong) NSString * name; \@end – Ryan Aug 17 '12 at 17:21
  • Yes, you can put the methods inside the class extension. However, I think under Xcode 4.4 you no longer need to declare your private methods. – J2theC Aug 17 '12 at 17:24
  • THANKS!!! 1. So what are the difference between using category to declare private methods than using extension? I see many people using category for this purpose in the past.. 2. Yes, there is no need in Xcode 4.4 but I just want to get rid of the warnings.. – Ryan Aug 17 '12 at 17:30
  • check this answer: http://stackoverflow.com/questions/7136124/class-extension-vs-class-category – J2theC Aug 17 '12 at 17:34
  • Thanks..but seems many people prefer using category to implement private method, like Google: http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/Networking/GDataServerError.m – Ryan Aug 18 '12 at 01:45