IOS Learning -- difference between @ class and # import,

Source: Internet
Author: User

IOS Learning -- difference between @ class and # import,

During iOS development, we often see # import as the import package in some source code, but sometimes we also see @ class as the import package, so what are the differences between the two methods?

I. Main differences between @ class and # import

First, let's talk about the main differences:

  • Use # import to introduce a Class header file. during compilation, all information in the Class header file will be introduced, including attributes and methods.;
  • Using @ class only tells the compiler that this is a classDoes not include other information about the class, but we only care about this. We do not need to know the Internal Attributes and methods of the class, therefore, using @ class can improve compilation performance.
2. Application scenarios of @ class

@ Class has two main application scenarios:

  • GenerallyUse @ class in the. h header file to Improve compilation PerformanceIn. in m implementation files, you often need to know the internal information of the class. In this case, you need to use # import to introduce this information. In many iOS source files. many of the hfiles use the @ class Method to export packages, for example, in the UIView. the H file contains the following code:
    @class UIBezierPath, UIEvent, UIWindow, UIViewController, UIColor, UIGestureRecognizer, UIMotionEffect, CALayer, UILayoutGuide;
  • ToAvoid loop reference between two classesIn the. h file of a class, we use @ class for import.

Next we will focus on the second scenario. For cyclic dependency, for example, Class A references class B and class B also references Class A. For example, in the following case, the code that we use # import for this nested reference will report an error during compilation.

//. H file # import "B. h "@ interface A: NSObject @ property (strong, nonatomic) B * _ B; @ end // B. h file # import ". h "@ interface B: NSObject @ property (strong, nonatomic) A * _ a; @ end

To solve this problem, we can use the @ class Method for reference,Use # import, use @ class, or use @ class to avoid errors..

//. H file @ class B @ interface A: NSObject @ property (strong, nonatomic) B * _ B; @ end // B. h file @ class A @ interface B: NSObject @ property (strong, nonatomic) A * _ a; @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.