0

我正在开发我的应用程序,但我需要一些帮助。我在Objective C中需要一个“类似php的会话”(不使用互联网连接)我考虑过全局变量,但我的应用程序似乎在重新加载视图时重置了它们。这是我当前的代码

SecondViewController.h

@interface SecondViewController : UIViewController
{
    NSString * string;
}

第二视图控制器.m

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (! [string isEqualToString:@"Hello"])
    {
        NSLog(@"Hello");
        string = @"Hello";
    }
    else
    {
        NSLog(@"Bye");
    }
}

@end

但是每次我重新加载 SecondViewController 时,“字符串”都会重置为其默认值。我正在寻找我们在 php 中使用的东西 (ae $_SESSION['string'] = 'hello')

4

1 回答 1

3

它可能对你有帮助。除非从您的设备中删除应用程序,否则它会存储这些值。

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

**Retrieving**

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
于 2013-05-06T08:58:57.757 回答