iOS NSObject类设置值

时间:2013-09-12 11:36:04

标签: ios objective-c getter-setter nsobject

我不知道我是完全厚实还是只是失明。我在设置我创建的成员类时遇到了问题。

Member.h

#import <Foundation/Foundation.h>

@interface Member : NSObject {

    NSInteger *MemberID;
    NSString *FirstName;
    NSString *LastName;
    NSString *EmailAddress;
    NSString *DeviceID;
    NSString *Password;
    BOOL *LoggedIn;
}

@property (nonatomic, assign) NSInteger *MemberID;
@property (nonatomic, retain) NSString *FirstName;
@property (nonatomic, retain) NSString *LastName;
@property (nonatomic, retain) NSString *EmailAddress;
@property (nonatomic, retain) NSString *DeviceID;
@property (nonatomic, retain) NSString *Password;
@property (nonatomic, assign) BOOL *LoggedIn;

- (id)initWithName:(NSInteger *)aMemberID FirstName:(NSString*)aFirstName LastName:(NSString *)aLastName EmailAddress:(NSString *)aEmailAddress DeviceID:(NSString *)aDeviceID  LoggedIn:(BOOL *)aLoggedIn Password: (NSString *)aPassword;


@end

Member.m

#import "Member.h"

@implementation Member
@synthesize FirstName, LastName, EmailAddress, DeviceID, LoggedIn, Password, MemberID;


- (id)initWithName:(NSInteger *)aMemberID FirstName:(NSString *)aFirstName LastName:(NSString *)aLastName EmailAddress:(NSString *)aEmailAddress DeviceID:(NSString *)aDeviceID LoggedIn:(BOOL *)aLoggedIn Password:(NSString *)aPassword
{

    self.MemberID = aMemberID;
    self.FirstName = aFirstName;
    self.LastName = aLastName;
    self.EmailAddress = aEmailAddress;
    self.DeviceID = aDeviceID;
    self.LoggedIn = aLoggedIn;
    self.Password = aPassword;
    return self;

}

// encoding for saving

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeInteger:self.MemberID forKey:@"MemberID"];
    [encoder encodeObject:self.FirstName forKey:@"FirstName"];
    [encoder encodeObject:self.LastName forKey:@"LastName"];
    [encoder encodeObject:self.EmailAddress forKey:@"EmailAddress"];
    [encoder encodeObject:self.DeviceID forKey:@"DeviceID"];
    [encoder encodeBool:self.LoggedIn forKey:@"LoggedIn"];
    [encoder encodeObject:self.Password forKey:@"Password"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.MemberID = [decoder decodeIntegerForKey:@"MemberID"];
        self.FirstName = [decoder decodeObjectForKey:@"FirstName"];
        self.LastName = [decoder decodeObjectForKey:@"LastName"];
        self.EmailAddress = [decoder decodeObjectForKey:@"EmailAddress"];
        self.DeviceID = [decoder decodeObjectForKey:@"DeviceID"];
        self.LoggedIn = [decoder decodeBoolForKey:@"LoggedIn"];
        self.Password = [decoder decodeObjectForKey:@"Password"];
    }
    return self;
}

@end

然后在appdelegate中,我正在设置成员类。

MyAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


    //set up the members item
    self.member = [Member alloc];
}

然后在我的视图控制器中,我引用了app委托并尝试使用值设置成员对象。我只是觉得它不能保存它吗?

//例如设置

appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.member.FirstName = @"Matt";
appDelegate.member.LastName = @"Facer";
appDelegate.member.EmailAddress = @"matt@facer.com";
appDelegate.member.MemberID = 12;
appDelegate.member.LoggedIn = YES;
appDelegate.saveLogin = [switchRememberLogin isOn];
NSLog(@"member name=%@",appDelegate.member.FirstName); //equals (NULL)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

首先尝试在appDelegate中初始化您的会员价值。

    self.member = [[Member alloc] init];

同时从*媒体资源中删除BOOL