• swift NSUserDefaults的基本使用


    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)
            }
            
        }
    
    }
    
  • 相关阅读:
    python基础集合
    python 布尔类型
    元组,内置函数
    python 字典类型用法
    python
    redis非关系数据库
    TensorFlow基础9——tensorboard显示网络结构
    TensorFlow基础8——结果可视化
    TensorFlow基础7——完整神经网络栗子
    TensorFlow基础6——函数定义(神经网络添加神经层函数)
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5160735.html
Copyright © 2020-2023  润新知