iOS属性列表(plist)示例
有很多更好的方法可以将持久性数据保留在我们的应用程序中,而不是对静态文本进行硬编码。
三种常用的方法是NSUserDefaults,CoreData和plist。
我们将在本教程中讨论并实现plist。
iOS属性列表
属性列表用于以结构化的方式存储一些数据,以后可以使用。
原始格式的属性列表为XML格式。
数据以带有键值对的字典的形式存储。
属性列表无法存储所有数据。
它们仅限于某些类型的数据,例如数组,字典,字符串,数字等。
在本教程中,我们将创建自己的属性列表,并将其保存并在表视图应用程序中使用。
创建名为PropertyLists的单视图应用程序类型的新项目。
在项目视图中,打开" Info.plist"。
它看起来应该像这样:
右键单击" Info.plist",然后选择"打开"作为源代码。
属性列表将以原始XML形式显示。
让我们创建自己的媒体资源列表。
创建一个新文件。
选择iOS-> Resource-> Property List,如下所示,并提供所需的名称。
要添加或者删除行,请按相应的按钮。
我们创建了一个由两个子数组组成的属性列表。
一个带有Mac OSX版本代码,另一个带有版本名称。
我们的属性列表文件如下所示:
我们将创建一个嵌入在导航控制器中的TableViewController。
在按下任何表格单元格时,将带我们到视图控制器,该控制器显示带有所按下单元格的相关版本名称的标签文本。
项目结构
项目视图由一个ViewController.swift文件(子类为UITableViewController),一个SecondViewController.swift文件和新创建的属性列表文件组成。
代码
从情节提要中删除ViewController并添加一个新的TableViewController。
将其嵌入到导航控制器中,并使导航控制器成为初始视图控制器。
添加带有标签文本的新View Controller。
将序列从UITableViewCell拖动到此ViewController。
情节提要板应该看起来像下面给出的那样。
声明ViewControllers,segue和UITableViewCell的相关标识符。
在UILabel的属性检查器中,选择自动换行。
下面给出了ViewController.swift源代码。
import UIKit class ViewController: UITableViewController { @IBOutlet var myTable: UITableView! let textCellIdentifier = "cell" var tableData = [String]() var tableValues = [String]() override func viewDidLoad() { super.viewDidLoad() //Do any additional setup after loading the view, typically from a nib. myTable.delegate = self myTable.dataSource = self let path = NSBundle.mainBundle().pathForResource("MyList", ofType: "plist") let dict = NSDictionary(contentsOfFile: path!) tableData = dict!.objectForKey("Version Code") as! [String] tableValues = dict!.objectForKey("OSX") as! [String] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() //Dispose of any resources that can be recreated. } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "mySegue" { let s1 = segue.destinationViewController as! SecondView let blogIndex = tableView.indexPathForSelectedRow?.row s1.pass = tableValues[blogIndex!] } } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell let row = indexPath.row cell.textLabel?.text = tableData[row] return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) } }
我们已将子类类型更改为UITableViewController,并实现了所有符合协议的方法。
tableview对象的委托和数据源被初始化为ViewController对象本身。
tableData和tableValues用属性列表文件中定义的两个数组初始化。
使用单击的UITableViewCell的索引将segue对象与版本名称的项目数组一起传递。
SecondViewController.swift在下面定义。
import UIKit class SecondView: UIViewController { var pass:String? @IBOutlet var label: UILabel! override func viewDidLoad() { super.viewDidLoad() if let name = pass { label.text=name } //Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() //Dispose of any resources that can be recreated. }