ios 如何在目标 c 中创建 BOOL 实例变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10685024/
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
How do I create a BOOL instance variable in objective c?
提问by Nosrettap
I need to create an instance variable of type BOOL
with a default value of False
in one of my Objective-C classes. How do I do this? I've seen people define it in their .h file but I don't really need this variable to be public. Thus shouldn't I put it in my .m file? Additionally, should I make it a property? Or should I ever not make something a property? Thanks.
我需要在我的 Objective-C 类之一中创建一个类型BOOL
为默认值的实例变量False
。我该怎么做呢?我见过人们在他们的 .h 文件中定义它,但我真的不需要这个变量是公开的。因此我不应该把它放在我的 .m 文件中吗?此外,我应该将其设为属性吗?或者我不应该把东西变成财产?谢谢。
回答by JeremyP
I need to create an instance variable of type BOOL with a default value of FALSE in one of my objective c classes. How do I do this?
我需要在我的目标 c 类之一中创建一个默认值为 FALSE 的 BOOL 类型的实例变量。我该怎么做呢?
Assuming you are using the current Xcode, you can declare the ivar in the implementation like this:
假设您使用的是当前的 Xcode,您可以在实现中声明 ivar,如下所示:
@implementation MyClass
{
@private
BOOL myBool;
}
I've seen people define it in their .h file but I don't really need this variable to be public.
我见过人们在他们的 .h 文件中定义它,但我真的不需要这个变量是公开的。
Historically, you had to declare instance variables in the @interface
because of the way classes were implemented. This is now necessary only if you are targeting 32 bit OS X.
过去,@interface
由于类的实现方式,您必须在 中声明实例变量。现在仅当您面向 32 位 OS X 时才需要这样做。
Additionally, should I make it a property?
此外,我应该将其设为属性吗?
That depends. You should definitely make it a property if it is part of your class's external API (for unrelated classes or subclasses to use. If it's only part of the object's internal state, you don't need to make it a property (and I don't).
那要看。如果它是您的类的外部 API 的一部分(供不相关的类或子类使用。如果它只是对象内部状态的一部分,则您不需要将其设为属性(我不需要) t)。
Or should I ever not make something a property? Thanks.
或者我不应该把东西变成财产?谢谢。
If you are not using ARC and the type is an object type (BOOL is not) you should always make it a property to take advantage of the memory management of the synthesized accessors. If you areusing ARC, the advice on the Apple developer list is to make stuff that is part of the API properties and stuff that is internal state as ivars.
如果您不使用 ARC 并且类型是对象类型(BOOL 不是),您应该始终将其设为属性以利用合成访问器的内存管理。如果您正在使用ARC,苹果开发者名单上的建议是做的东西,是API性能和东西,是内部状态的ivars的一部分。
Note that, even for internal state, you might want to use KVO, in which case, use a property.
请注意,即使对于内部状态,您也可能希望使用 KVO,在这种情况下,请使用属性。
If you declare this BOOL as a property and synthesize it, you do not need to explicitly declare the ivar. If you want to make the property "visible" only to the class itself, use a class extension
如果将此 BOOL 声明为属性并对其进行合成,则无需显式声明 ivar。如果您想让该属性仅对类本身“可见”,请使用类扩展
@interface MyClass()
@property (assign) BOOL myBool;
@end
@implementation MyClass
@synthesize myBool = myBool_; // creates an ivar myBool_ to back the property myBool.
@end
Note that, although the compiler will produce warnings for the above, at run time, any class can send setMyBool:
or myBool
to objects of MyClass
.
请注意,尽管编译器会针对上述情况产生警告,但在运行时,任何类都可以将setMyBool:
或发送myBool
到MyClass
.
回答by graver
If you want to make a private variable you can use the power of categories. Make a class MyClass for example and in the .m file do the following:
如果你想创建一个私有变量,你可以使用类别的力量。例如,创建一个类 MyClass 并在 .m 文件中执行以下操作:
#import "MyClass.h"
@interface MyClass() //This is an empty category on MyClass class
@property (nonatomic, assign) BOOL myBool;
@end
@implementation MyClass
@synthesize myBool = _myBool;
-(void)myMethod {
self.myBool = YES; //this is using the property
_myBool = NO; //this is the instance variable, as @synthesize creates an instance variable for you
// both are private
}
@end
回答by CodaFi
BOOL's are declared as iVars with a simple BOOL myBool
, and as a property with @property (nonatomic, assign) BOOL myBool
. Take note though, BOOLean values are inititalized to NO (nil). If you need a BOOL to be accessible from other classes, or "global" in the m file, use an iVar or a property, otherwise just declare them as scoped vars in an individual method.
BOOL 声明为带有简单 的 iVar BOOL myBool
,并带有@property (nonatomic, assign) BOOL myBool
. 但请注意,BOOLEan 值初始化为 NO (nil)。如果您需要 BOOL 可从其他类访问,或在 m 文件中“全局”,请使用 iVar 或属性,否则只需在单个方法中将它们声明为作用域变量。
回答by Matthias
You can put the declaration in the .m
file.
您可以将声明放在.m
文件中。
Whether you make a property out of it, depends on what you want. As a property, you can later apply setter/getter that include checking/guards/whatever. In addition, you can apply key-value-access.
你是否用它来创造财产,取决于你想要什么。作为一个属性,您可以稍后应用包括检查/守卫/任何内容的 setter/getter。此外,您可以应用键值访问。
If you do not need one of this, there is no real need for an property (but a good tradition: you never know, how your code will evolve).
如果您不需要其中之一,那么就没有真正需要属性(但是一个好的传统:您永远不知道您的代码将如何发展)。
回答by Pfitz
If you make it a property it is public. If you do not want the variable to be public, than do not use a property. Why do you not want to be public? You can have readonly property and make the writable in you .m via a private category.
如果你让一个属性它是公共的。如果您不希望变量公开,则不要使用属性。为什么不想公开?您可以拥有 readonly 属性并通过私有类别使您在 .m 中可写。
You can put it in your .m file if you use iOS 5
如果你使用 iOS 5,你可以把它放在你的 .m 文件中
@implementation MyClass {
BOOL _myBool;
}
I think this is new to iOS5. But I have seen code like this too :
我认为这是 iOS5 的新功能。但我也见过这样的代码:
@implementation MyClass {
@private
BOOL _myBool;
}
Hope this helps.
希望这可以帮助。