281

@privateObjective-Cではどういう意味ですか?

4

3 に答える 3

186

これは可視性修飾子です。つまり、として宣言されたインスタンス変数には、同じクラスのインスタンスからの@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では常に公開されています。ただし、メソッド宣言を「非表示」にする方法はいくつかあります。詳細については、この質問を参照してください。

于 2009-05-10T04:07:27.170 に答える
160

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の2つのインスタンスが、互いのすべてのivarに直接アクセスできることに注意してください。プログラマーはこのクラスを直接完全に制御できるため、この能力を賢く使用すると想定されています。

于 2009-05-16T06:14:32.150 に答える
15

@privateインスタンス変数にアクセスできないと言われたら、それが何を意味するのかを理解することが重要です。実際には、ソース コードでこれらの変数にアクセスしようとすると、コンパイラからエラーが返されます。GCC と XCode の以前のバージョンでは、エラーではなく警告が表示されます。

いずれにせよ、実行時には、すべての賭けが無効になります。これら@private@protectedivar は、任意のクラスのオブジェクトからアクセスできます。これらの可視性修飾子は、ソース コードを可視性修飾子の意図に違反するマシン コードにコンパイルすることを困難にします。

セキュリティのために ivar 可視性修飾子に依存しないでください! 彼らはまったく何も提供しません。これらは厳密に、クラス ビルダーの希望をコンパイル時に強制するためのものです。

于 2012-10-02T23:54:03.733 に答える