当我打电话给方法时没有任何反应

时间:2014-03-31 18:39:12

标签: ios xcode nsuserdefaults

我有一个课程,我存储我的方法:

.h 档案:

   #import <Foundation/Foundation.h>

@interface SessionController : NSObject

-(void) addSession :(NSString *)userName
      tipulContului:(NSString *)tipCont;
-(void) destroySession : (NSString *) userName;
-(NSString *) getSessionUsername;
-(NSString *) getSessionTipCont;
@end

.m 档案:

  #import "SessionController.h"

    @implementation SessionController

    -(void) addSession : (NSString *)userName
         tipulContului :(NSString *)tipCont
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:userName forKey:@"SessionUsername"];
        [defaults setObject:tipCont forKey:@"SessionTipCont"];
        NSLog(@"------------------------------------------------");
        //NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
        NSLog(@"%@", userName);

        [defaults synchronize];
    }
    -(void) destroySession : (NSString *) userName
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:@"SessionUsername"];
        [defaults removeObjectForKey:@"SessionTipCont"];
        [defaults synchronize];
    }
    -(NSString *) getSessionUsername
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        //  verificam existenta utilizatorului
        if([defaults stringForKey:@"SessionUsername"]!=nil && ![[defaults stringForKey:@"SessionUsername"] isEqual:@""]){
            return [defaults stringForKey:@"SessionUsername"];
        }
        else
            return nil;
    }
    -(NSString *) getSessionTipCont
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        //  verificam existenta Tipului de cont
        if([defaults objectForKey:@"SessionTipCont"]!=nil && ![[defaults stringForKey:@"SessionTipCont"] isEqual:@""]){
            return [defaults stringForKey:@"SessionTipCont"];
        }
        else
            return nil;
    }
    @end

比我打电话从外部类调用方法

SessionController *sessionController;
    [sessionController addSession:name tipulContului:type];

但是addSession方法中的 NSLog 没有被调用,我发送给方法的值没有存储,它编译,我没有任何警告,我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要初始化sessionController对象

SessionController* sessionController = [[SessionController alloc] init];

答案 1 :(得分:1)

方法名称前面的连字符表示方法是&#34;实例&#34;方法。您需要实例化对象才能使用它们。你差不多这样做了。尝试:

 SessionController *sessionController = [[SessionController alloc] init];