xcode 用作全局变量的 AppDelegate 变量不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10243635/
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
Variable of AppDelegate used as global variable doesn't work
提问by animal_chin
I would like to use my AppDelegate to store a object which will be accessible to any other classes. I've declared this AppDelegate like this :
我想使用我的 AppDelegate 来存储任何其他类都可以访问的对象。我已经像这样声明了这个 AppDelegate:
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
{
MyClass *tracker;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ICViewController *viewController;
@property (retain, nonatomic) MyClass *tracker;
@end
I synthesize tracker and in application:didFinishLaunchingWithOptions: i set one NSString in that object like this :
我合成跟踪器并在 application:didFinishLaunchingWithOptions: 中设置一个 NSString 在该对象中,如下所示:
self.tracker = [[MyClass alloc] init];
self.tracker.testGlobal = @"global funguje";
When i need to access tracker somewhere else in some other class i use this code :
当我需要在其他某个类的其他地方访问跟踪器时,我使用以下代码:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
MyClass *trackerTEST = appDelegate.tracker;
NSLog(@"TEST : %@",trackerTEST.testGlobal);
The problem is that testGlobal is NULL. What am I doing wrong? Also here are class files for MyClass :
问题是 testGlobal 是 NULL。我究竟做错了什么?这里还有 MyClass 的类文件:
@interface MyClass : NSObject
{
NSString *testGlobal;
}
@property (retain, nonatomic) NSString *testGlobal;
@end
@implementation MyClass
@synthesize testGlobal = _testGlobal;
@end
Thanks for any kind of help!
感谢您的任何帮助!
回答by CarlJ
maybe I'm answer to late but, you can take a look at the Singleton Pattern.
也许我回答晚了,但是你可以看看Singleton Pattern。
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
在软件工程中,单例模式是一种设计模式,用于通过将类的实例化限制为一个对象来实现单例的数学概念。当只需要一个对象来协调整个系统的操作时,这很有用。这个概念有时被推广到当只有一个对象存在时更有效地运行的系统,或者将实例化限制为一定数量的对象。
Here is an ObjC implementation: http://getsetgames.com/2009/08/30/the-objective-c-singleton/
这是一个 ObjC 实现:http://getsetgames.com/2009/08/30/the-objective-c-singleton/
回答by DocJones
Check if application:didFinishLaunchingWithOptions:
is being called by adding a NSLog(@"...")
. If trackerTEST
is nil, it was possibly not correctly initialized.
检查是否application:didFinishLaunchingWithOptions:
正在通过增加一个叫做NSLog(@"...")
。如果trackerTEST
为 nil,则可能未正确初始化。
回答by Jakub
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
{
MyClass *tracker;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ICViewController *viewController;
@property (retain, nonatomic) MyClass *tracker;
@end
Why you have @property tracker and var tracker?
为什么你有@property 跟踪器和 var 跟踪器?
Try to remove MyClass *tracker;
property only should be enough.
尝试删除MyClass *tracker;
属性应该就足够了。
回答by PrimaryChicken
Change @synthesize testGlobal = _testGlobal;
to @synthesize testGlobal;
and try again.
更改@synthesize testGlobal = _testGlobal;
为@synthesize testGlobal;
并重试。