这就是我想做的..
我有一个名为 userInfo 的类。我在另一个名为 LoginInfo 的类中创建了该对象的实例。我想让这个实例保持事件状态并可供所有其他类访问,直到应用程序处于事件状态为止...
我该如何实现这个目标?我在某处读到我可以用单例类来做到这一点。但我不知道它们是什么......我对 cocoa 很陌生......请指导......
提前致谢..
<小时/>@interface UserInfo : NSObject {
NSString * firstName;
NSString * lastName;
NSString * uID;
NSString * password;
NSString * userType;
}
-(id)initWithFirstName:(NSString *)fname andLastName:(NSString *)lname andUID:(NSString *)userID andPassword:(NSString *)pwd andUserType:(NSString *)type;
@property (readwrite, copy) NSString * firstName;
@property (readwrite, copy) NSString * lastName;
@property (readwrite, copy) NSString * uID;
@property (readwrite, copy) NSString * password;
@property (readwrite, copy) NSString * userType;
@end
#import "UserInfo.h"
@implementation UserInfo
-(id)initWithFirstName:(NSString *)fname andLastName:(NSString *)lname andUID:(NSString *)usid andPassword:(NSString *)pwd andUserType:(NSString *)type{
self=[super init];
if (self) {
self.firstName=fname;
self.lastName=lname;
self.uID=usid;
self.password=pwd;
self.userType=type;
}
return self;
}
@synthesize firstName;
@synthesize lastName;
@synthesize uID;
@synthesize password;
@synthesize userType;
@end
这是我想要创建单例的类...请指导我必须进行哪些更改..我想使用自定义构造函数... 很抱歉将此代码作为答案。但我无法在评论中得到它......
最佳答案
什么是单例: Yeah.. I know.. I'm a simpleton.. So what's a Singleton?
如何在 Objective-C 中实现它: What should my Objective-C singleton look like?
关于objective-c - 如何让对象在整个应用程序中保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4884625/