[IOS] 使用属性列表实现数据持久化

//
//  ViewController.swift
//  just
//
//  Created by Stary on 4/4/16.
//  Copyright © 2016 Stary. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    // array of switches
    @IBOutlet var Switches : [UISwitch]!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let filePath = self.dataFilePath()
        // judge if the path is available
        if (NSFileManager.defaultManager().fileExistsAtPath(filePath)) {
            let array = NSArray(contentsOfFile: filePath) as! [Bool]
            for var i = 0; i < array.count; i++ {
                Switches[i].on = array[i]
            }
        }

        let app = UIApplication.sharedApplication()
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "applicationWillResignActive:",
            name: UIApplicationWillResignActiveNotification,
            object: app)
    }
    // receive the message
    // Sent when the application is about to move from active to 
    // interruptions (such as an incoming phone call or SMS message) or when the user quits the application 
    // and it begins the transition to the background state.
    func applicationWillResignActive(notification:NSNotification) {
        let filePath = self.dataFilePath()
        let array = (self.Switches as NSArray).valueForKey("on") as! NSArray
        array.writeToFile(filePath, atomically: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 寻找文件路径
   func dataFilePath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(
        NSSearchPathDirectory.DocumentDirectory,
        NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory = paths[0] as NSString
    return documentsDirectory.stringByAppendingPathComponent("data.plist") as String
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C# MAUI 中,可以使用键值对存储来进行持久存储。键值对存储是一种简单的存储方式,它允许你使用键和对应的值来存储和检索数据。以下是一些常见的键值对存储选项: 1. 应用程序设置:可以使用 `App.Current.Properties` 属性来存储应用程序的设置和配置信息。这是一个键值对集合,你可以通过添加、更新和删除键值对来存储和检索数据。这些设置将会持久保存,即使应用程序被关闭并重新打开。 ```csharp // 存储设置 App.Current.Properties["key"] = value; // 获取设置 if (App.Current.Properties.ContainsKey("key")) { var value = App.Current.Properties["key"]; } // 删除设置 if (App.Current.Properties.ContainsKey("key")) { App.Current.Properties.Remove("key"); } ``` 2. SharedPreferences(Android)/ NSUserDefaults(iOS):对于 Android 平台,可以使用 `Preferences` 类来进行键值对存储。对于 iOS 平台,可以使用 `NSUserDefaults` 类来进行键值对存储。这些存储方式允许你以类似的方式存储和检索数据。 ```csharp // 存储数据(Android) Preferences.Set("key", value); // 获取数据(Android) var value = Preferences.Get("key", defaultValue); // 删除数据(Android) Preferences.Remove("key"); ``` ```csharp // 存储数据iOS) NSUserDefaults.StandardUserDefaults.SetString(value, "key"); // 获取数据iOS) var value = NSUserDefaults.StandardUserDefaults.StringForKey("key"); // 删除数据iOS) NSUserDefaults.StandardUserDefaults.RemoveObject("key"); ``` 3. 文件存储:你也可以将键值对数据存储在文件中,例如使用 JSON、XML 或其他格式进行序列。然后,使用文件操作 API 来进行读写操作。 这些是一些常见的键值对存储选项。选择适合你需求的存储方式取决于数据的复杂性和应用程序的需求。如果数据较为简单且量较小,可以使用应用程序设置或平台特定的键值对存储。如果数据较为复杂或需要高级查询和操作,可以考虑使用文件存储或数据库。记得在进行持久存储时,要考虑数据的安全性和隐私性,确保对敏感数据进行适当的保护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值