在阅读了对另一个答案的评论后
我需要在多个类中获取 userEMail 值,因此向每个类发送数据是错误的
听起来你更像是在单例课程之后。所以尝试这样的事情
AuthObject.h
@interface AuthObject : NSObject
@property (nonatomic, strong) NSString *userEMail;
+ (AuthController*)authInstance;
@end
AuthObject.m
#import "AuthObject.h"
@implementation AuthObject
@synthesize userEMail = _userEMail;
static AuthObject *authObjectSharedInstance = nil;
+ (AuthObject *)authInstance
{
static dispatch_once_t instanceToken;
dispatch_once(&instanceToken, ^{
authObjectSharedInstance = [[AuthObject alloc] init];
});
return authObjectSharedInstance;
}
@end
然后在另一个类的另一个方法中,只要你已经导入了AuthObject.h
你可以做的地方
- (void)someMethodIMadeUpInClass1
{
AuthObject *authObj = [AuthObject authInstance];
[authObj setUserEMail:@"myemail@address.com"];
}
然后在一个完全不同的课程中你可以做
- (void)someMethodIMadeUpInClass2
{
AuthObject *authObj = [AuthObject authInstance];
NSLog(@"my email : %@", [authObj userEMail];
}
从理论上讲,如果您正在创建一个单例类而不通过sharedInstance
( authInstance
here),那么该代码将被破坏。试图隐藏这种破碎只会在以后引起痛苦。这就是为什么我会选择单例类而不是使用NSUserDefaults
.