ios - 调用 plist 的子项
<p><p>这是我的属性(property) list :<a href="/image/hAyf6.png" rel="noreferrer noopener nofollow">directory.plist</a> </p>
<p>我想知道如何将子项调用到 ViewController 中。这是我的实际 ViewController:</p>
<pre><code>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()
})
}
}
</code></pre>
<p>这是我正在使用的结构:</p>
<pre><code>import UIKit
struct Employee {
let position: String
let name: String
let email: String
let phone: String
init(dictionary: ) {
self.position = dictionary["Position"] ?? ""
self.name = dictionary["Name"] ?? ""
self.email = dictionary["Email"] ?? ""
self.phone = dictionary["Phone"] ?? ""
}
}
struct Shared {
static var instance = Shared()
var employees: = []
}
</code></pre>
<p>在 AppDelegate 里面我放了这个:</p>
<pre><code> func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -> Bool {
if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [] {
Shared.instance.employees = array.map{Employee(dictionary: $0)}
}
return true
</code></pre>
<p>我必须调用我的 directory.plist 上的子项吗?我的意思是<strong>关键细节</strong>里面的项目。然后我想展示 Func1、Func2 和 Func3。</p>
<p><em>obs.:(这个func是functionary的缩写)</em></p>
<p>谢谢!</p>
<hr/>
<p>经过一些更改,现在我的子项为零:<a href="/image/sxjrw.png" rel="noreferrer noopener nofollow">debugger</a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您在应用程序委托(delegate)中的代码很好。您只需要使用另一个属性更新您的 <code>Employee</code> 结构来存储 <code>Details</code>。但这也意味着您的员工字典不仅仅是字符串值的字典。所以你需要更新代码来处理正确的值类型:</p>
<pre><code>struct Employee {
let position: String
let name: String
let email: String
let phone: String
let details:
init(dictionary: ) {
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? ) ?? [:]
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 调用 plist 的子项,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43035137/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43035137/
</a>
</p>
页:
[1]