我有一个NSString *日期。我从互联网获取一些数据,现在我有这个变量的日期。我希望能够在任何时候使用这个NSString,而无需调用在我的程序中的任何范围内返回它的方法。单身人士目标C
我在哪里可以将代码从互联网中检索变量将保存的日期?它会在这里吗?然后我最终会使date = sharedInstance?
static SingletonClass *sharedInstance = nil;
// Get the shared instance and create it if necessary.
+ (SingletonClass *)sharedInstance
{
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
//Do I Put Code here?????
return sharedInstance;
}
在此先感谢!
你不需要一个单例来做到这一点。 – Steve 2012-03-03 18:53:18
'我希望能够在不调用方法的情况下随时使用这个NSString' - 你在这里描述一个全局变量,而不是一个单例。另外,你为什么要这样做,为什么你想避免调用一个方法?大多数情况下,如果您的设计使用全局变量或单例,则可以进行改进。 – 2012-03-03 19:02:39
[创建一个变量,最终将保存一段数据以供所有方法使用]的可能的重新发布(http://stackoverflow.com/questions/9548556/creating-a-variable-that-will-eventually-hold-一块数据将被使用的所有) – 2012-03-03 19:40:45