• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 调用 plist 的子项

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:21:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的属性(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



Best Answer-推荐答案


您在应用程序委托(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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap