OC learning --- class extension, oc --- Extension
In the previous article, we introduced the concept and usage of category: Category
To put it bluntly, extension is to make up forForward DeclarationWe know that in C, if you want to call a function, you must declare a function before that, which is frontend.
In OC, in order to make up for this problem in C language, there is an extended concept. Let's take a look at the code below:
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. It is also okay to save @ interface Person (private)-(void) _ showInfo; @ end @ implementation Person {// defines the attribute here. This attribute is also private. // However, the difference between this method and the @ private method previously defined is one of the two. the one defined in the H file is. // defined in the m file is open if it is open source. h and. m file // if it is a closed source, it is generally only open. h file}-(void) work {// This line of code is now compiled and there is no problem with calling it in versions earlier than Xcode, because if we call a method before implementation in C, we must declare it before. // here, we use extension technology to implement private methods. // Of course, the version after Xcode fixes this issue, so the purpose of the extension is not very big now. // It is equivalent to the forward Declaration [self _ showInfo] in C language; NSLog (@ "started working");} // Private method, only in. m file defined, not in. it is declared in the H file, which is generally used in the class-(void) _ showInfo {NSLog (@ "my info");} @ end
Here we can see that in Person. in the header file h, we define the work method in Person. the implementation of this method in the m file, but we call the _ showInfo method in the work method. If it is in a version earlier than Xcode or a feature in the C language, the _ showInfo method should be declared before work, so here there is an extension:
Extended Definition:
@ Interface Person (private)
-(Void) _ showInfo;
@ End
Before the class definition, of course, the private here can also be omitted, so that the work method can be called. However, the latest Xcode has made up for this problem, so the extended functions are almost negligible. Here is just an introduction to the extension in OC, and it may not be used in the future.
Summary
In this article, we will describe the extended concepts in OC. The extended functions in OC can be ignored. You just need to know about them and will not be able to use this function in the future.