안녕하세요.
옵씨로 개발할때 쓰던 @class 가 뭔지 궁금해서 정리한 글입니다.
Forward declarations 라고 불린다고 하네요. (참고로 옵씨에만 있는 문법이 아니라 c++등에서도 사용되네요)
https://stackoverflow.com/questions/5191487/objective-c-forward-class-declaration
위의 스택오버플로우 번역하고 마지막에 정리해놓았습니다.
시간 없으신분들은 맨 아래에 정리한것만 보시면 편할거에요.
첫번째 해석
It basically tells the compiler that the class RootViewController exists, without specifying what exactly it looks like (ie: its methods, properties, etc). You can use this to write code that includes RootViewController member variables without having to include the full class declaration.
This is particularly useful in resolving circular dependencies - for example, where say ClassA has a member of type ClassB*, and ClassB has a member of type ClassA*. You need to have ClassB declared before you can use it in ClassA, but you also need ClassA declared before you can use it in ClassB. Forward declarations allow you to overcome this by saying to ClassA that ClassB exists, without having to actually specify ClassB's complete specification.
Another reason you tend to find lots of forward declarations is some people adopt a convention of forward declaring classes unless they absolutely must include the full declaration. I don't entirely recall, but possibly that's something that Apple recommends in it's Objective-C guiding style guidlines.
Continuing my above example, if your declarations of ClassA and ClassB are in the files ClassA.h and ClassB.h respectively, you'd need to #import whichever one to use its declaration in the other class. Using forward declaration means you don't need the #import, which makes the code prettier (particularly once you start collecting quite a few classes, each of which would need an `#import where it's used), and increases compiling performance by minimising the amount of code the compiler needs to consider while compiling any given file.
As an aside, although the question is concerned solely with forward declarations in Objective-C, all the proceeding comments also apply equally to coding in C and C++ (and probably many other languages), which also support forward declaration and typically use it for the same purposes.
---------
It basically tells the compiler that the class RootViewController exists, without specifying what exactly it looks like (ie: its methods, properties, etc).
- 기본적으로 컴파일러에게 class RootViewConrtoller가 존재하는지 알려준다. 정확히 어떻게 생겼는지는 알려주지 않는다.(메소드나 프로퍼티는 가르쳐 주지 않는다.)
You can use this to write code that includes RootViewController member variables without having to include the full class declaration.
- 이를 사용하면 전체 클래스를 선언하지 않고 RootViewController의 멤버변수를 포함하는 코드를 작성할 수 있다.
This is particularly useful in resolving circular dependencies - for example, where say ClassA has a member of type ClassB*, and ClassB has a member of type ClassA*.
- 이는 순환 종속성을 해결하는데 특히 유용하다. 예를들어. ClassA에는 ClassB*의 멤버를 갖고있고 ClassB에는 ClassA*의 멤버를 갖고 있다.
You need to have ClassB declared before you can use it in ClassA, but you also need ClassA declared before you can use it in ClassB.
- 당신은 Class B선언하기 전에 ClassA를 선언해야 사용할수있다. 하지만 당신은 Class A를 선언하기 전에 Class B를 선언해야 한다.
Forward declarations allow you to overcome this by saying to ClassA that ClassB exists, without having to actually specify ClassB's complete specification.
- Forward declarations은 극복할수 있도록 허용해준다. ClassB의 완전한 내용을 모르더라도 ClassB가 존재한다고 ClassA에게 말해준다.
Another reason you tend to find lots of forward declarations is some people adopt a convention of forward declaring classes unless they absolutely must include the full declaration.
- forward declarations를 많이 찾고(사용)하는 또다른 이유는 전체 선언을 반드시 포함해야 하는 경우가 아니라면 클래스 선언 규칙을 채택하는 사람들도 있다.
Continuing my above example, if your declarations of ClassA and ClassB are in the files ClassA.h and ClassB.h respectively, you'd need to #import whichever one to use its declaration in the other class.
- 위의 예제를 계속 진행하면, ClassA 와 Class B 선언이 각각 ClassA.h, ClassB.h파일에 있는 경우 다른 클래스에서 선언을 사용하려면 #import를 가져와야 한다.
Using forward declaration means you don't need the #import, which makes the code prettier
- "forward declaration"를 사용하면 #import를 사용할 필요가 없고 예쁜 코드를 작성할 수 있다.
increases compiling performance by minimising the amount of code the compiler needs to consider while compiling any given file.
- 주어진 파일을 컴파일하는 동안 컴파일러가 고려해야하는 코드의 양을 최소화하여 컴파일 성능을 향상 시킨다.
두번째 해석
Forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc. Basically when you import a file, contents of the file are substituted at the point of import when you build your project, which is then fed to the compiler. If you have circular imports, you'd have an infinite loop which would never compile. Fortunately xcode will tell you about this before trying. The forward declaration says "Don't import this class but just know that it exists. " Without either an import or a forward declaration, you get an error that no such class exists.
한줄씩 해석
Forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc
- Forward declarations은 주로 circular imports(순환참조)를 피하기 위해 사용된다. 순환참조란 A 파일이 B 파일을 import하는데 B파일에서 A파일과 다른 것들을 import 하는것이다.
Basically when you import a file, contents of the file are substituted at the point of import when you build your project, which is then fed to the compiler.
- 기본적으로 당신이 파일을 import할때, import한 파일의 내용은 프로젝트를 빌드 할 때 컴파일러에게 공급된다.
If you have circular imports, you'd have an infinite loop which would never compile.
- 만약 당신이 순환참조를 하고 있을때 무한루프에 빠져 컴파일이 끝나지 않는다. (A -> B를 참조하고 B -> A 를 참조하면 무한참조)
Fortunately xcode will tell you about this before trying.
- 다행히 xCode는 컴파일 하기 전에 사용자에게 말을 해준다.
The forward declaration says "Don't import this class but just know that it exists.
- forward declaration은 "이 클래스를 import하면 안된다. 하지만 그 클래스가 존재한다고 알려는 줘라" 라고 말한다.
Without either an import or a forward declaration, you get an error that no such class exists.
- import나 forward declaration 둘 중 하나라도 없으면 당신은 클래스가 없다는 에러를 받게 될것이다.
정리
- #import 를 사용하여 클래스를 import하면 전체 파일을 컴파일을 하는데 @class는 클래스가 있다는 것만 알려주므로 빌드 속도가 빨라진다.
- 순환참조를 피하기 위해 사용된다.
'공부 > iOS' 카테고리의 다른 글
Objective-C typedef에 대해서 (0) | 2020.06.09 |
---|---|
iOS 계층구조 (0) | 2020.06.04 |
Swift - typealias (0) | 2020.05.20 |
UISplitViewContrller(1) - 기초, displayMode, master의 width조절 (0) | 2020.03.26 |
[오류] Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug (0) | 2020.02.05 |