import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // Override point for customization after application launch. self.window!.backgroundColor = UIColor.whiteColor() self.window!.rootViewController = RootViewController(); self.window!.makeKeyAndVisible() return true } }
import UIKit class RootViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let key:String = "isFirst" //读取(值不为true,则是第一次登录) let isFirst:Bool = NSUserDefaults.standardUserDefaults().boolForKey(key) as Bool! print(isFirst) //如果是第一次登录存储数据,否则读取数据 if(isFirst == false){ print("第一次登录") //存储 NSUserDefaults.standardUserDefaults().setObject(true , forKey: key) NSUserDefaults.standardUserDefaults().setObject(1, forKey:"number") NSUserDefaults.standardUserDefaults().setObject("甘雨路", forKey: "name") //设置同步 NSUserDefaults.standardUserDefaults().synchronize() }else{ //读取 let num = NSUserDefaults.standardUserDefaults().objectForKey("number") as! Int print(num) let name = NSUserDefaults.standardUserDefaults().objectForKey("name") as! String print(name) } } }