美文网首页
@class和#import,define和static con

@class和#import,define和static con

作者: li_礼光 | 来源:发表于2017-03-06 14:37 被阅读24次

@class和#import

什么时候用@class这种方式声明比#import好呢?
Randy Marsh:
When I develop, I have only three things in mind that never cause me any problems.
    1. Import super classes   引入子类
    2. Import parent classes (when you have children and parents)  引入父类
    3. Import classes outside your project (like in frameworks and libraries)  引入第三方框架的类
For all other classes (subclasses and child classes in my project self), I declare them via forward-class.
 
Justin:
Simple answer: You #import or #include when there is a physical dependency. Otherwise, you use forward declarations (@class MONClass ,struct MONStruct , @protocol MONProtocol ).
Here are some common examples of physical dependence:
    • Any C or C++ value (a pointer or reference is not a physical dependency). If you have aCGPoint as an ivar or property, the compiler will need to see the declaration ofCGPoint .
    • Your superclass.
    • A method you use.

什么时候用#import

1.有继承关系
2.使用第三方框架的时候

什么时候用@class

简单来说就是除了以上的情况之外都可以.

PS : 这个只是在.h文件中考虑是用@class还是#import,考虑点是编译的效率,如果使用了@class,在.m文件中还是需要#import相应的头文件

要点 :
• 除非确有必要,否则不要引入头文件.一般来说,应在某个类的头文件中使用向前声明来提及别的类,并在实现文件中引入那些类的头文件.这样做可以尽量降低类之间的耦合
• 有时无法使用向前声明,比如要声明某个类遵循一项协议.在这种情况下.尽量把"该类遵循某协议"的这条声明移至"class-continuation分类"中.如果不行的话,就把协议单独放在一个头文件中,然后将其引入.


define和static const

一般定义一个固定的常量,会使用

#decine ANIMATION_DURATION 0.3

这种的方式存在一种隐式的危险,如果当前类被其他类引用,而其他类也正好有ANIMATION_DURATION,那么所有ANIMATION_DURATION都会被替换.(其实出现这种情况的概率很低)

static const NSTimeInterval kAnimationDuration = 0.3;

static : 表示kAnimationDuration在内存的常量区,只要值不变,内存空间不会发生变化.
const : 表示kAnimationDuration是只读状态

实际上,如果一个变量既声明static,又声明const没那么编译器根本不会创建符号,而是会像#define预处理指令一样,把所有遇到的变量都替换为常量值.不过还是要记住:用这种方式定义的常量带有类型信息.

什么时候用#define
什么时候用static const

这两个什么时候,或者什么情况下用都是一样的.一个是引入的一些小的风险细节,一个是带有类型信息.


extern

extern在C语言中,用在变量或者函数的声明前,用来说明"此变量/函数是在别处定义的,要在此处引用"

如何理解:
iOS extern使用教程

简单粗暴的理解 :
在一个类中使用extern声明某个变量,就是告诉编译器,在内存中去找这个变量的值.至于这个变量声明在哪个类中并不重要.

你也可以随便extern一个变量名,比如:
extern NSString *asda
只要不调用.编译器都不会报错.因为只是告诉编译器,将会有一个这样的变量在内存中.你记得去找就可以了,当你调用之后,编译器会报错,告诉你,抱歉,我在内存中没找到这个变量.

什么时候使用extern合理.
这个在自己的项目中慢慢揣摩...方式有很多,也可以不用.

相关文章

  • @class和#import,define和static con

    @class和#import 什么时候用#import 什么时候用@class PS : 这个只是在.h文件中考虑...

  • Class 和 import

    https://blog.csdn.net/zanshiyonghuming/article/details/50...

  • @class和#import

    1>>>@class作用 仅仅告诉编译器,某个名称是一个类 举例 @class Person;//仅仅告诉编译器P...

  • class和static

    相同点:都可以修饰func ,static 修饰的方法叫做静态方法,class修饰的叫做类方法都可以修饰计算属性 ...

  • static 和 class

    在swift中 static和class都是用来表示”类型范围作用域“,但是在oc中并不会特别区分,而在swift...

  • static 和 class

    Swift 中表示“类型范围作用域”这一概念的有两个不同的关键字, static 和 class 在非 class...

  • static 和 class

    介绍 Swift中表示 “类型范围作用域” 这一概念有两个不同的关键字,它们分别是static和class。这两个...

  • flutter 坐标转化 bd09-wg84 等

    import 'dart:math'; class GpsUtil {static const num pi = ...

  • 键盘录入数据,并求最大值

    import java.util.Scanner; class Test{ public static void ...

  • 我的乌龟流星雨

    import java.awt.*; public class java{ public static void ...

网友评论

      本文标题:@class和#import,define和static con

      本文链接:https://www.haomeiwen.com/subject/pfcugttx.html