全球可变值问题iPhone



我已经声明了一个变量NSString sessionId;在应用程序委托文件中供全局使用。

在 appdelegate.h 中

@property (strong, nonatomic) NSMutableString *sessionId;

在appdelegate.m

@synthesize sessionId;

并像这样在我的一个视图控制器中使用它。

    ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;
NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);

我能够在我所有的视图控制器中访问该变量 sessionId,但问题是该值未保存在其中。就像在viewcontroller1.m中一样,我给它分配了一些值,但是当我在另一个viewcontroller2.m中访问sessionId时,它在控制台上将其值显示为null。

如何解决此问题。

添加的代码:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
self.receivedData = nil;

    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    NSArray *session = [json objectWithString:responseString error:&error];
    SBJsonParser *parser =[[SBJsonParser alloc] init];
    NSDictionary *jsonObject = [parser objectWithString:responseString];
    sID = [jsonObject objectForKey:@"session"];
    ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID;
    NSLog(@"session id = %@", ((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId);

    NSUserDefaults *sessID = [NSUserDefaults standardUserDefaults];
    [sessID setObject:sID forKey:@"sessionID"];
    [sessID synchronize];
    NSLog(@"This session %@ saved", sID);
    NSDictionary *accountsObj = [parser objectWithString:responseString];
    NSString *accounts = [accountsObj objectForKey:@"account"];
    NSArray *accountList = [accounts componentsSeparatedByString:@","];
    NSLog(@"Accounts: %@", accountList);

    if (session == nil)
    NSLog(@"JSON parsing failed: %@", [error localizedDescription]);
else {
    if (accountList.count > 1) {
        ChooseAccountsController *accountsView = [[ChooseAccountsController alloc] initWithNibName:@"ChooseAccountsController" bundle:nil ];
        self.chooseAcc = accountsView;
        [self.view addSubview: accountsView.view];
    } else if (accountList.count == 1) {
            MainScreenController *mainScreen1 = [[MainScreenController alloc] initWithNibName:@"MainScreen" bundle:nil ];
             self.mainScreen = mainScreen1;
             [self.view addSubview: mainScreen1.view];
         }
    }
}
((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;

不是无效代码,请尝试

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID

强属性应该保留,并且保留是一条消息,因此如果您需要此语法(在这种情况下不是(,唯一正确的语法是[anObject retain]

尝试

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = [jsonObject objectForKey:@"session"];

老实说,不知道为什么这不起作用,但尝试创建一个单例对象来保存和访问此变量。这更优雅,应该符合您的意图。

NSLog 在 viewcontroller1 中查看其值以确保其设置。

如果是,则很可能在 viewcontroller2 中,当您尝试访问会话 ID 时,您正在重新定义会话 ID。

此外,您可能希望在应用程序委托之外创建单一实例。

我认为这条线

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID.retain;

应改为

((VektorAppDelegate *)[UIApplication sharedApplication].delegate).sessionId = sID;

我想知道它根本没有崩溃......

最新更新