[ios] Objective-C에서“@private”은 무엇을 의미합니까?

@privateObjective-C의 의미는 무엇입니까 ?



답변

그것은 A의 가시성 수정 인스턴스 변수로 선언 – 그것은 수단 것을 @private에만 액세스 할 수 있습니다 같은 클래스의 인스턴스 . 서브 클래스 또는 다른 클래스는 개인 멤버에 액세스 할 수 없습니다.

예를 들면 다음과 같습니다.

@interface MyClass : NSObject
{
    @private
    int someVar;  // Can only be accessed by instances of MyClass

    @public
    int aPublicVar;  // Can be accessed by any object
}
@end

또한 명확히하기 위해 메소드는 항상 Objective-C에서 공개됩니다. 메서드 선언을 “숨기는”방법 있습니다. 자세한 내용 은 이 질문 을 참조하십시오.


답변

htw가 말했듯이 가시성 수정 자입니다. @private즉, ivar (인스턴스 변수)는 동일한 클래스의 인스턴스 내에서만 직접 액세스 할 수 있습니다. 그러나 이는 그다지 의미가 없을 수 있으므로 예를 들어 보겠습니다. init단순화를 위해 클래스 의 메소드를 예제로 사용합니다 . 관심있는 항목을 지적하기 위해 인라인으로 댓글을 달겠습니다.

@interface MyFirstClass : NSObject
{
    @public
    int publicNumber;

    @protected  // Protected is the default
    char protectedLetter;

    @private
    BOOL privateBool;
}
@end

@implementation MyFirstClass
- (id)init {
    if (self = [super init]) {
        publicNumber = 3;
        protectedLetter = 'Q';
        privateBool = NO;
    }
    return self;
}
@end

@interface MySecondClass : MyFirstClass  // Note the inheritance
{
    @private
    double secondClassCitizen;
}
@end

@implementation MySecondClass
- (id)init {
    if (self = [super init]) {
        // We can access publicNumber because it's public;
        // ANYONE can access it.
        publicNumber = 5;

        // We can access protectedLetter because it's protected
        // and it is declared by a superclass; @protected variables
        // are available to subclasses.
        protectedLetter = 'z';

        // We can't access privateBool because it's private;
        // only methods of the class that declared privateBool
        // can use it
        privateBool = NO;  // COMPILER ERROR HERE

        // We can access secondClassCitizen directly because we 
        // declared it; even though it's private, we can get it.
        secondClassCitizen = 5.2;
    }
    return self;
}

@interface SomeOtherClass : NSObject
{
    MySecondClass *other;
}
@end

@implementation SomeOtherClass
- (id)init {
    if (self = [super init]) {
        other = [[MySecondClass alloc] init];

        // Neither MyFirstClass nor MySecondClass provided any 
        // accessor methods, so if we're going to access any ivars
        // we'll have to do it directly, like this:
        other->publicNumber = 42;

        // If we try to use direct access on any other ivars,
        // the compiler won't let us
        other->protectedLetter = 'M';     // COMPILER ERROR HERE
        other->privateBool = YES;         // COMPILER ERROR HERE
        other->secondClassCitizen = 1.2;  // COMPILER ERROR HERE
    }
    return self;
}

따라서 귀하의 질문에 대답하기 위해 @private는 다른 클래스의 인스턴스가 ivar을 액세스하지 못하도록 보호합니다. MyFirstClass의 두 인스턴스는 서로의 모든 ivar에 직접 액세스 할 수 있습니다. 프로그래머가이 클래스를 직접 완벽하게 제어 할 수 있기 때문에이 기능을 현명하게 사용할 것입니다.


답변

누군가가 @private인스턴스 변수에 액세스 할 수 없다고 말할 때의 의미를 이해하는 것이 중요 합니다. 실제로 소스 코드에서 이러한 변수에 액세스하려고하면 컴파일러에서 오류가 발생합니다. 이전 버전의 GCC 및 XCode에서는 오류 대신 경고가 표시됩니다.

어느 쪽이든, 런타임에 모든 베팅이 해제됩니다. 이 클래스 @private@protectedivar은 모든 클래스의 객체로 액세스 할 수 있습니다. 이러한 가시성 수정자는 가시성 수정 자의 의도를 위반하는 머신 코드로 소스 코드를 컴파일하기 어렵게 만듭니다.

보안을 위해 ivar 가시성 수정 자에 의존하지 마십시오! 그들은 전혀 제공하지 않습니다. 그들은 클래스 빌더의 소원을 컴파일 타임으로 시행하기위한 것입니다.


답변