ios “@private”在Objective-C 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/844658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:54:37  来源:igfitidea点击:

What does "@private" mean in Objective-C?

iosobjective-ccocoaprivate

提问by

What does @privatemean in Objective-C?

@private在Objective-C中是什么意思?

采纳答案by hbw

It's a visibility modifier—it means that instance variables declared as @privatecan only be accessed by instances of the same class. Private members cannot be accessed by subclasses or other classes.

它是一个可见性修饰符——这意味着声明为 的实例变量@private只能被同一类的实例访问。子类或其他类不能访问私有成员。

For example:

例如:

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

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

Also, to clarify, methods are always public in Objective-C. There are ways of "hiding" method declarations, though—see this questionfor more information.

另外,澄清一下,Objective-C 中的方法总是公开的。不过,有一些方法可以“隐藏”方法声明——有关更多信息,请参阅此问题

回答by BJ Homer

As htw said, it's a visibility modifier. @privatemeans that the ivar (instance variable) can only be accessed directly from within an instance of that same class. However, that may not mean much to you, so let me give you an example. We'll use the initmethods of the classes as examples, for simplicity's sake. I'll comment inline to point out items of interest.

正如 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;
}

So to answer your question, @private protects ivars from access by an instance of any other class. Note that two instances of MyFirstClass could access all of each other's ivars directly; it is assumed that since the programmer has complete control over this class directly, he will use this ability wisely.

所以为了回答你的问题,@private 保护 ivars 不被任何其他类的实例访问。请注意,MyFirstClass 的两个实例可以直接访问彼此的所有变量;假设程序员可以直接完全控制这个类,他会明智地使用这个能力。

回答by Jeff Wolski

It important to understand what it means when somebody says that you can't access a @privateinstance variable. The real story is that the compiler will give you an error if you attempt to access these variables in your source code. In previous versions of GCC and XCode, you would just get a warning instead of an error.

当有人说您无法访问@private实例变量时,理解这意味着什么很重要。真实的情况是,如果您尝试访问源代码中的这些变量,编译器会报错。在以前版本的 GCC 和 XCode 中,您只会收到警告而不是错误。

Either way, at run time, all bets are off. These @privateand @protectedivars can be accessed by an object of any class. These visibility modifiers just make it difficult to compile the source code into machine code that violates the intent of the visibility modifiers.

无论哪种方式,在运行时,所有赌注都将关闭。这些@private@protectedivars 可以被任何类的对象访问。这些可见性修饰符只会使将源代码编译成违反可见性修饰符意图的机器代码变得困难。

Do not rely on ivar visibility modifiers for security! They provide none at all. They are strictly for compile-time enforcement of the class-builder's wishes.

不要依赖 ivar 可见性修饰符来确保安全!他们根本不提供。它们严格用于编译时强制执行类构建器的愿望。