简体   繁体   English

在目标C中创建全局变量

[英]Creating global variables in Objective C

I have designed a login app. 我设计了一个登录应用程序。 I have entered the account data in sqlite database. 我已经在sqlite数据库中输入了帐户数据。 However when I logged in with the account username and password i want these to be accessible to all view controllers. 但是,当我使用帐户用户名和密码登录时,我希望所有视图控制器都可以访问它们。 Because, I have different controllers which will update the account details. 因为,我有不同的控制器将更新帐户详细信息。 Need implementation details.. Thanks 需要实现细节。。谢谢

Use a singleton: http://www.galloway.me.uk/tutorials/singleton-classes/ 使用单例: http : //www.galloway.me.uk/tutorials/singleton-classes/

I have a project with something like this (note I'm using the iOS keychain rather than a database, but I've hidden those details here): 我有一个类似这样的项目(请注意,我使用的是iOS钥匙串而不是数据库,但我在这里隐藏了这些详细信息):

@interface User : NSObject
@property (nonatomic, strong) NSString *username;

+ (User *)currentUser;

@end

@implementation User

+ (User *)currentUser
{
    static User *currentUser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentUser = [[User alloc] initFromKeychain];
    });
    return currentUser;
}
@end

There are A LOT of approaches to doing singletons (depending on how paranoid you want to be about whether someone can create a non-singleton instance of your class or not). 有很多方法可以做单例(取决于您对某人是否可以创建您的类的非单例实例的偏执程度)。 Examples here: What should my Objective-C singleton look like? 这里的示例: 我的Objective-C单例应该是什么样?

You could also just access the keychain directly wherever you need it. 您也可以在需要的地方直接访问钥匙串。 You should also ask yourself why so much of your code needs access to a username/password. 您还应该问自己,为什么这么多代码需要访问用户名/密码。 Sounds like a broken design. 听起来像是破损的设计。

Avoid global variables, rather you can use a Singleton class to store application wide credentials information. 避免使用全局变量,而是可以使用Singleton类来存储应用程序范围的凭据信息。

However keep in mind that the best way to store credentials in an app is using Apple Keychain, you can find the API reference on the Apple site. 但是请记住,在应用程序中存储凭据的最佳方法是使用Apple钥匙串,您可以在Apple网站上找到API参考。

As it was said - avoid global variables, but if you want them for sure - do it like in c language: 就像说的那样-避免使用全局变量,但是如果您确实需要它们,请像使用c语言一样进行操作:

define this variable in one file 在一个文件中定义此变量

in all other files refer to it with extern keyword 在所有其他文件中,使用extern关键字引用它

Simply: Don't do it! 简单:不要这样做!

I recommend to store the credentials in the keychain. 我建议将凭据存储在钥匙串中。 But don't use the sample code Apple provides. 但是不要使用Apple提供的示例代码。 It doesn't work. 没用 I use SSKeychain in my projects. 我在项目中使用SSKeychain

If you don't need to store the credentials you should, as aleroot suggested, use a singleton or a class with only one instance. 如果您不需要存储凭据,则应按照aleroot的建议,使用单例或仅包含一个实例的类。

Three answers: 三个答案:

  • Don't do it. 不要这样
  • If you must do it, use a Singleton. 如果必须这样做,请使用Singleton。
  • For extremely easy access to username and non confidential (ie, password) variables, use NSUserDefaults. 要非常轻松地访问用户名和非机密(即密码)变量,请使用NSUserDefaults。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM