objective-c iphone上Objective C中的静态字符串变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/980083/
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
Static string variable in Objective C on iphone
提问by Yogini
How to create & access static string in iPhone (objective c)?
I declare static NSString *str = @"OldValue"in class A.
如何在 iPhone 中创建和访问静态字符串(目标 c)?我static NSString *str = @"OldValue"在A级声明。
If i assign some value to this in class B as str = @"NewValue".
This value persists for all methods in class B. But if I access it in class C (after assignment in B) I am getting it as OldValue.
Am I missing something? Should i use extern in other classes?
如果我在 B 类中为此分配一些值作为str = @"NewValue". 这个值对于 B 类中的所有方法都存在。但是如果我在 C 类中访问它(在 B 中赋值之后),我将它作为 OldValue 获取。我错过了什么吗?我应该在其他课程中使用 extern 吗?
Thanks & Regards, Yogini
感谢和问候, Yogini
回答by zpasternack
Update:As of Xcode 8, Objective-C doeshave class properties. Note, it's mostly syntactic sugar; these properties are not auto-synthesized, so the implementation is basically unchanged from before.
更新:从 Xcode 8 开始,Objective-C确实具有类属性。注意,它主要是语法糖;这些属性不是自动合成的,所以实现与之前基本没有变化。
// MyClass.h
@interface MyClass : NSObject
@property( class, copy ) NSString* str;
@end
// MyClass.m
#import "MyClass.h"
@implementation MyClass
static NSString* str;
+ (NSString*) str
{
return str;
}
+ (void) setStr:(NSString*)newStr
{
if( str != newStr ) {
str = [newStr copy];
}
}
@end
// Client code
MyClass.str = @"Some String";
NSLog( @"%@", MyClass.str ); // "Some String"
See WWDC 2016 What's New in LLVM. The class property part starts at around the 5 minute mark.
请参阅WWDC 2016 LLVM 中的新增功能。类属性部分从大约 5 分钟开始。
Original Answer:
原答案:
Objective-C doesn't have class variables, which is what I think you're looking for. You can kinda fake it with static variables, as you're doing.
Objective-C 没有类变量,这就是我认为您正在寻找的。你可以用静态变量来伪造它,就像你在做的那样。
I would recommend putting the static NSString in the implementation file of your class, and provide class methods to access/mutate it. Something like this:
我建议将静态 NSString 放在类的实现文件中,并提供类方法来访问/改变它。像这样的东西:
// MyClass.h
@interface MyClass : NSObject {
}
+ (NSString*)str;
+ (void)setStr:(NSString*)newStr;
@end
// MyClass.m
#import "MyClass.h"
static NSString* str;
@implementation MyClass
+ (NSString*)str {
return str;
}
+ (void)setStr:(NSString*)newStr {
if (str != newStr) {
[str release];
str = [newStr copy];
}
}
@end
回答by Quinn Taylor
Unlike Java, where a static variable is scoped for all instances of a class, staticin C means that a variable is accessible only from within the file where it is declared. It allows you to do things like declare a static variable inside a function, which sets the value only the first time through, like this.
与 Java 不同,静态变量的作用域是类的所有实例,static在 C 中意味着变量只能从声明它的文件中访问。它允许你做一些事情,比如在函数内声明一个静态变量,它只在第一次通过时设置值,像这样。
One thing you haven't mentioned is the relationship between classes A, B, and C. If they are in an inheritance hierarchy, and you're expecting the static variable to be inherited as in Java, the method described by zpasternackwill work.
您没有提到的一件事是类 A、B 和 C 之间的关系。如果它们在继承层次结构中,并且您希望像在 Java 中一样继承静态变量,那么zpasternack描述的方法将起作用。
If the three classes are unrelated, and you just want to access the value declared in A, then externis a more appropriate way to go. In this case, you want to declare the variable as externin ClassA.h, then define it in Class.m. As long as ClassB and ClassC import ClassA.h, they will be able to link against the same extern definition.
如果这三个类不相关,而您只想访问 A 中声明的值,那么extern是一种更合适的方法。在这种情况下,您希望extern在 ClassA.h 中声明变量,然后在 Class.m 中定义它。只要 ClassB 和 ClassC 导入 ClassA.h,它们就能够链接到相同的 extern 定义。
One fine point is that, instead of using externby itself, it's more robust to use OBJC_EXPORT, which is defined in objc-api.h and handles compiling under C++ as well. Here's a code sample:
一个很好的一点是,它不是单独extern使用,而是使用更健壮OBJC_EXPORT,它在 objc-api.h 中定义并且也处理在 C++ 下的编译。这是一个代码示例:
// ClassA.h
OBJC_EXPORT NSString* commonString;
...
// ClassA.m
NSString* commonString = @"OldValue";
// ClassB.m
#import "ClassA.h"
...
commonString = @"NewValue"; // Can be inside a function or method
Of course, using externed variables in this way creates an infamous, much-maligned global variable, which is fragile in that anyone can read or write it, and access is uncontrolled. This is the simple approach, and answers your question about using staticvs. extern. However, as a design principle, the encapsulation provided by wrapping the variable with class methods is much safer, albeit more complex. In object-oriented languages, when the effect you're trying to achieve is that of a class-static method, encapsulation is probably the right way to go.
当然,以这种方式使用外部变量会创建一个臭名昭著、备受诟病的全局变量,它的脆弱性在于任何人都可以读取或写入它,并且访问不受控制。这是一种简单的方法,可以回答您关于使用staticvs.的问题extern。但是,作为设计原则,使用类方法包装变量提供的封装更安全,尽管更复杂。在面向对象的语言中,当您要实现的效果是类静态方法的效果时,封装可能是正确的方法。

