How to read from a plist with Swift 3 iOS app

前端 未结 7 1902
無奈伤痛
無奈伤痛 2020-12-24 06:49

-Disclaimer-
I\'m extremely new to iOS and Swift development, but I\'m not particularly new to programming.

I have a basic iOS applicati

7条回答
  •  梦谈多话
    2020-12-24 07:09

    In AppDelegate File

    var bundlePath:String!
        var documentPath:String!
        var plistDocumentPath:URL!
        let fileManager = FileManager()
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
        {
            bundlePath = Bundle.main.path(forResource: "Team", ofType: "plist")
    
            documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
    
            plistDocumentPath = URL.init(string: documentPath)?.appendingPathComponent("Team.plist")
            print(plistDocumentPath.path)
    
            if !fileManager.fileExists(atPath: plistDocumentPath.path){
    
                do {
                    try fileManager.copyItem(atPath: bundlePath, toPath: plistDocumentPath.path)
                } catch  {
                    print("error Occured \(error.localizedDescription)")
                }
    
            }
    
    
            return true
        }
    

    In ViewController

     @IBOutlet weak var TeamTable: UITableView!
        var appDelegate:AppDelegate!
        var arrayForContacts:[[String:Any]]! // array object
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
    
    
            appDelegate = UIApplication.shared.delegate as! AppDelegate
    
    
        }
        override func viewWillAppear(_ animated: Bool) {
    
            super.viewWillAppear(animated)
    
            if appDelegate.fileManager.fileExists(atPath: appDelegate.plistDocumentPath.path){
                arrayForContacts = []
                if let contentOfPlist = NSArray.init(contentsOfFile: appDelegate.plistDocumentPath.path ){
                    arrayForContacts = contentOfPlist as! [[String:Any]]
                    TeamTable.reloadData()
                }
    
            }
        }
    

提交回复
热议问题