这是我的属性(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["osition"] ?? ""
self.name = dictionary["Name"] ?? ""
self.email = dictionary["Email"] ?? ""
self.phone = dictionary["hone"] ?? ""
}
}
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是functionary的缩写)
谢谢!
经过一些更改,现在我的子项为零: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["osition"] as? String) ?? ""
self.name = (dictionary["Name"] as? String) ?? ""
self.email = (dictionary["Email"] as? String) ?? ""
self.phone = (dictionary["hone"] as? String) ?? ""
self.details = (dictionary["Details"] as? [String:String]) ?? [:]
}
}
关于ios - 调用 plist 的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035137/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |