One feature in OC: extension
In fact, the extension is to make up for the C language
Forward Statement, we know that in the C language, if you want to invoke a function, then you have to 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
[OBJC]View Plaincopy
1.//
2.//Person.h
3.//10_categoryextend
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import <Foundation/Foundation.h>
10.
11.
@interfacePerson:nsobject
12.
13.-(
void) work;
14.
15.
@end
person.m
[OBJC]View Plaincopy
1.//
2.//PERSON.M
3.//10_categoryextend
4.//
5.//Created by Jiangwei on 14-10-11.
6.//Copyright (c) Jiangwei 2014. All rights reserved.
7.//
8.
9. #import "Person.h"
10.
11.//This is called extension
12.//or private omit is not a problem
13.
@interfacePerson (
Private)
14.-(
void) _showinfo;
15.
@end
16.
17.
@implementationperson{
18.//define properties Here, this property is also private
19.//But the difference between this approach and the previously defined @private
20.//The difference between the two is one defined in the. h file, one that is defined in the. m file.
21.//If it is open source, it is generally open. h and. m files
22.//If it is closed source, generally only open. h file
23.}
24.
25.-(
void) work{
26.//This line of code is not a problem to compile now, in the previous version of Xcode is problematic, because we in the C language before the implementation of the method to call, it must be declared before
27.//Here we use extension technology to implement private methods
28.//Of course, the version after Xcode fixes the problem, so the use of extensions is not very large now
29.//equivalent to a forward declaration in the C language
30. [
Self_showinfo];
NSLog (@ "started working");
32.}
33.
34.//Private method, which is defined in. m files, is not declared in the. h file and is generally used in the class itself.
35.-(
void) _showinfo{
NSLog (@ "My Information");
37.}
38.
39.
@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.
SummaryIn 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.
One feature in OC: extension