Monday, November 7, 2016

difference bw category & extension

Category:
1. The category .h file is optional. When it is deleted the .m file have to import the main .h file
2.  To expose the method, it has to be declared in the .h file.
3. Can't declare any new property in the category.

Extension:
1. Kinda like category,  but anonymous, what is even better,  it can declare the new property!
2. Can be used to expose read-only properties.
for example:

in main .m file, declare the internal method

@interface Account ()

@property (nonatomic, readonly) NSString* randomStuff;
+ (void)setCurrentAccount:(Account*)account;

then implement the method
+ (void)setCurrentAccount:(Account*)account
{
    currentAccount = account;}


in other class's .m file, set extension interface to expose the method to the current class.


@interface Account ()
+ (void)setCurrentAccount:(Account*)account;@end

later in this class, we will be able to call the private method.

- (void)cleanCurrentUser{
    [Account setCurrentAccount:nil];
}

No comments:

Post a Comment