开发者

Keeping an object alive across the application-How?

Here is what i want to do..

I have a class called userInfo. I create an instance of this object in another class called LoginInfo. I want to keep this instace alive and accessible to all other classes and till the time the application is alive...

How do i achieve this? I read somewhere that i can do this with singleton classes. But i have no idea what they are...I am quite new to cocoa..Please guide..

Thanks in advance..


@interface UserInfo : NSObjec开发者_开发问答t {
    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

This is the class i want to make singleton.... Please guide as to what changes i have to make..I want to use the custom constructor... M sorry to put this code as an answer. But i could not get it in a comment...


What a Singleton is: Yeah.. I know.. I'm a simpleton.. So what's a Singleton?

How to implement it in Objective-C: What should my Objective-C singleton look like?


This sounds indeed like a small description of the Singleton Pattern. One way to implement a Singleton is to access it's functionality via class-methods; these class-methods access the single instance as a private class-member, creating it if it's not already there.

I can't help you with the cocoa syntax (objective-c if I'm not mistaken), here is some pseudo-code to illustrate one possible implementation:

class Singleton {
    /* class member */
    private static Singleton instance = undef;

    /* class methods */
    public static type1 doSomething() {
        Singletong instance = Singleton::getInstance();
        return instance->reallyDoSomething();
    }

    private static Singleton getInstance() {
       if( !defined(Singleton::instance)) {
            Singleton:instance = new Singleton();
       }
       return Singleton::instance;
    }

    // instance method
    private type1 reallyDoSomething() {
       type1 result;
       /* exciting stuff */
       return result;
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜