调用plist的子项

时间:2017-03-26 22:06:03

标签: ios swift plist

这是我的财产清单:debugger

我想知道如何将子项调用到ViewController中。这是我的实际ViewController:

import UIKit

class Page1: UIViewController {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var positionLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

        Shared.instance.employees.forEach({

            nameLabel.text = (("name:", $0.name) as? String)

            print("name:", $0.name)
            print("position:", $0.position)
            print("phone:", $0.phone)
            print("email:", $0.email)
            print()
        })
    }
}

这就是我正在使用的结构:

import UIKit

struct Employee {
    let position: String
    let name: String
    let email: String
    let phone: String
    init(dictionary: [String: String]) {
        self.position = dictionary["Position"] ?? ""
        self.name = dictionary["Name"] ?? ""
        self.email = dictionary["Email"] ?? ""
        self.phone = dictionary["Phone"] ?? ""
    }
}

struct Shared {
    static var instance = Shared()
    var employees: [Employee] = []
}

在AppDelegate中,我把它放在:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String: String]] {
            Shared.instance.employees = array.map{Employee(dictionary: $0)}
    }
    return true

我是否必须在我的directory.plist上调用子项目?我指的是关键详情中的项目。然后我想展示Func1,Func2和Func3。

obs。:(此func是职员的缩写)

谢谢!

经过一些更改,现在我得到的子项目为零https://cloud.google.com/endpoints/docs/quickstart-app-engine

1 个答案:

答案 0 :(得分:0)

您在应用程序委托中的代码很好。您只需要使用其他属性更新Employee结构以存储Details。但这也意味着您的员工字典不仅仅是字符串值的字典。因此,您需要更新代码以处理正确的值类型:

struct Employee {
    let position: String
    let name: String
    let email: String
    let phone: String
    let details: [String:String]

    init(dictionary: [String: Any]) {
        self.position = (dictionary["Position"] as? String) ?? ""
        self.name = (dictionary["Name"] as? String) ?? ""
        self.email = (dictionary["Email"] as? String) ?? ""
        self.phone = (dictionary["Phone"] as? String) ?? ""
        self.details = (dictionary["Details"] as? [String:String]) ?? [:]
    }
}