2016년 5월 11일 수요일

Objective-C @class 의 용법

@class XYPoint; // 외부에서 선언 클래스를 사용할때 쓰고 컴파일러로 하여금 해당 클래스의 존재 여부만 확인하기 위한 용도
                // #import 해당 파일의 내용을 전부 붙여넣는거고 차이점은, @class 어딘가에 선언되어 있으니 참조하라는 의미

                // -> 상호 #import 인한 재컴파일 하는 것을 미연에 방지하기 위함


그렇다고 해서 영원히 import를 하지 않아도 된다는 소리는 아님
 -> 실제로 해당 클래스의 구현부 내용이 필요할 때 (ex, 메소드 호출 시) import를 해줘야 함

If you say @class MyCoolClass, the compiler knows that it may see something like:
MyCoolClass *myObject;
It doesn't have to worry about anything other than MyCoolClass is a valid class, and it should reserve room for a pointer to it (really, just a pointer). Thus, in your header, @class suffices 90% of the time.
However, if you ever need to create or access myObject's members, you'll need to let the compiler know what those methods are. At this point (presumably in your implementation file), you'll need to #import "MyCoolClass.h", to tell the compiler additional information beyond just "this is a class".

댓글 없음:

댓글 쓰기