In the previous article we introduced the class concept and use: http://blog.csdn.net/jiangwei0910410003/article/details/41775329, then this article we continue to introduce a feature in OC: extension
In fact, the extension is to make up for the C language of the forward declaration , we know that in C, if you want to call a function, then you must declare a function before this, that is, there is a predecessor.
OC in order to make up for this problem in the C language, there is the concept of extension, the following look at the code:
Person.h
person.h// 10_categoryextend//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei . All rights reserved.//#import <Foundation/Foundation.h> @interface person:nsobject-(void) work; @end
person.m
person.m// 10_categoryextend//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei . All rights reserved.//#import "Person.h"//This is called extension//or private omit is no problem @interface person (private) -(void) _ Showinfo; @end @implementation person{ //define properties Here, this property is also private //But the difference between this method and the previously defined @private The difference between the two is defined in the. h file, which is defined in the. m file ///If it is open source, it is generally open. h and. m files //If it is closed source, generally only open. h file}-(void) work{ // This line of code now compiles is no problem, before Xcode version of the call is problematic, because we in the C language before the implementation of the method to call, we have to declare before// Here we use extension technology to implement private method // Of course, the later version of Xcode fixes the problem, so the extension is not very useful now //equivalent to the C language of the forward declaration [self _showinfo]; NSLog (@ "start working");} The private method, which is defined in the. m file, is not declared in the. h file and is generally used in the class itself-(void) _showinfo{ NSLog (@ "My Information");} @end
Here we see that in the Person.h header file, we define the work method, the implementation of this method in the Person.m file, but we call the _showinfo method in the work method, if the version before Xcode or the C language features, The _showinfo method should be declared before work, so there is an extension:
Definition of extension:
@interface Person (private)
-(void) _showinfo;
@end
Before the class definition, of course, the private here can also be omitted, so that in the work method can be called. But the latest Xcode has made up the problem, so the extended functionality can be almost negligible. This is just a brief introduction to the extension of OC, which may be almost impossible to use later.
Summarize
In this article, the concept of extension in OC is explained, the extension of OC is almost negligible, as long as the understanding can be, in the future, almost do not use this function.
Extension of---Classes of OC learning articles