菜鸟教程小白 发表于 2022-12-11 19:21:12

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 = ((&#34;name:&#34;, $0.name) as? String)

            print(&#34;name:&#34;, $0.name)
            print(&#34;position:&#34;, $0.position)
            print(&#34;phone:&#34;, $0.phone)
            print(&#34;email:&#34;, $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[&#34;Position&#34;] ?? &#34;&#34;
      self.name = dictionary[&#34;Name&#34;] ?? &#34;&#34;
      self.email = dictionary[&#34;Email&#34;] ?? &#34;&#34;
      self.phone = dictionary[&#34;Phone&#34;] ?? &#34;&#34;
    }
}

struct Shared {
    static var instance = Shared()
    var employees: = []
}
</code></pre>

<p>在 AppDelegate 里面我放了这个:</p>

<pre><code>    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {
      if let url = Bundle.main.url(forResource: &#34;directory&#34;, withExtension: &#34;plist&#34;), 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[&#34;Position&#34;] as? String) ?? &#34;&#34;
      self.name = (dictionary[&#34;Name&#34;] as? String) ?? &#34;&#34;
      self.email = (dictionary[&#34;Email&#34;] as? String) ?? &#34;&#34;
      self.phone = (dictionary[&#34;Phone&#34;] as? String) ?? &#34;&#34;
      self.details = (dictionary[&#34;Details&#34;] 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]
查看完整版本: ios - 调用 plist 的子项