gpt4 book ai didi

ios - 调用 plist 的子项

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:41 24 4
gpt4 key购买 nike

这是我的属性(property) list :directory.plist

我想知道如何将子项调用到 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 上的子项?我指的是 key Details 中的项目。然后我想展示 Func1、Func2 和 Func3。

obs.:(这个func是functionary的缩写)

谢谢!


经过一些更改,现在我得到了子项目 nil:debugger

最佳答案

您在应用程序委托(delegate)中的代码没有问题。您只需要使用另一个属性更新您的 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]) ?? [:]
}
}

关于ios - 调用 plist 的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com