700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > ios plist_iOS属性列表(plist)示例

ios plist_iOS属性列表(plist)示例

时间:2024-07-27 18:24:19

相关推荐

ios plist_iOS属性列表(plist)示例

ios plist

There are many better ways to keep persistent data in our application rather than hardcoding the static texts. Three commonly used ways areNSUserDefaults,CoreData,plist. We’ll discuss and implement plist in this tutorial.

有许多更好的方法可以将持久性数据保留在我们的应用程序中,而不是对静态文本进行硬编码。 三种常用的方法是NSUserDefaultsCoreDataplist。 我们将在本教程中讨论并实现plist。

iOS属性列表 (iOS Property List)

Property Listare used to store some data in a structural way that can be used on later. In raw form the property lists are in XML format. The data is stored in the form of a dictionary with key value pairs. Property lists can’t store all kinds of data. They are limited to certain types of data such as arrays, dictionaries, strings, Numbers etc. In this tutorial we’ll going to create our own property lists and save and use them in a table view application.

属性列表用于以结构化的方式存储一些数据,以后可以使用。 原始格式的属性列表为XML格式。 数据以带有键值对的字典的形式存储。 属性列表不能存储所有数据。 它们仅限于某些类型的数据,例如数组,字典,字符串,数字等。在本教程中,我们将创建自己的属性列表,并将其保存并在表视图应用程序中使用。

Create a new Project of the type Single View Application namedPropertyLists. In the project view open theInfo.plist. It should look like this:

创建一个名为PropertyLists的单视图应用程序类型的新项目。 在项目视图中,打开Info.plist。 它看起来应该像这样:

Right click theInfo.plistand select open as Source code. The Property lists will be seen in raw XML form.

右键单击Info.plist然后选择open作为源代码。 属性列表将以原始XML形式显示。

Let’s create our own Property Lists. Create a new file. Choose iOS -> Resource -> Property List as shown below and give the desired name.

让我们创建自己的属性列表。 创建一个新文件。 选择iOS-> Resource-> Property List,如下所示,并提供所需的名称。

To add or delete rows press the respective buttons. We’ve created a property list that consists of two child arrays. One withMac OSXVersion Codes and the other with the Version Names.

Our Property lists file looks like this:

要添加或删除行,请按相应的按钮。 我们创建了一个由两个子数组组成的属性列表。 一个带有Mac OSX版本代码,另一个带有版本名称。

我们的属性列表文件如下所示:

We’ll create a TableViewController that’s embedded in a Navigation Controller. On pressing any tableview cell it takes us to a View Controller that displays a label text with the relevant Version Name of the cell pressed.

我们将创建一个嵌入在导航控制器中的TableViewController。 在按下任何表格单元格时,将带我们到视图控制器,该控制器显示带有所按下单元格的相关版本名称的标签文本。

项目结构 (Project Structure)

The project view consists of a ViewController.swift file (that’s subclassed to UITableViewController), a SecondViewController.swift file and the newly created property lists file.

项目视图由一个ViewController.swift文件(子类化为UITableViewController),一个SecondViewController.swift文件和新创建的属性列表文件组成。

码 (Code)

Delete the ViewController from the storyboard and add a new TableViewController. Embed it in a Navigation Controller and make the Navigation Controller the initial view controller. Add a new View Controller with a label text. Drag a segue from the UITableViewCell to this ViewController. The Storyboard should look like the one given below.

从情节提要中删除ViewController并添加一个新的TableViewController。 将其嵌入到导航控制器中,并使导航控制器成为初始视图控制器。 添加带有标签文本的新View Controller。 将序列从UITableViewCell拖动到此ViewController。 情节提要板应类似于以下给出的内容。

Declare the relevant identifiers for the ViewControllers, the segue and the UITableViewCell. In the attributes inspector of the UILabel choose word wrap.

声明ViewController,segue和UITableViewCell的相关标识符。 在UILabel的属性检查器中,选择自动换行。

The ViewController.swift source code is given below.

下面给出了ViewController.swift源代码。

import UIKitclass 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 = selfmyTable.dataSource = selflet 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! SecondViewlet blogIndex = tableView.indexPathForSelectedRow?.rows1.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 UITableViewCelllet row = indexPath.rowcell.textLabel?.text = tableData[row]return cell}override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {tableView.deselectRowAtIndexPath(indexPath, animated: true)}}

We’ve changed the subclass type to UITableViewController and implemented all the methods conforming to the protocol.我们已经将子类类型更改为UITableViewController,并实现了所有符合协议的方法。 The tableview object delegate and datasource are initialised to the ViewController objectself.将tableview对象的委托和数据源初始化为ViewController对象self。 The tableData and tableValues are initialised with the two arrays defined in the property lists file.tableData和tableValues用属性列表文件中定义的两个数组初始化。 The segue object is passed with the item array of the version name using the index of the UITableViewCell clicked.使用单击的UITableViewCell的索引,将segue对象与版本名称的项目数组一起传递。

The SecondViewController.swift is defined below.

SecondViewController.swift在下面定义。

import UIKitclass 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.}

The output of the application in action is given below.

实际应用程序的输出如下。

This brings an end to this tutorial. You can download the iOS Property List Project from the below link.

本教程到此结束。 您可以从下面的链接下载iOS属性列表项目。

Download iOS Property List Project下载iOS属性列表项目

翻译自: /10623/ios-property-list-plist-example

ios plist

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。