Info.plist. Read Content of a Property File in Swift

In this short Swift code example, I am going to share with you how to load the content of a property file into NSDictionary. And in this particular example, I will load the content of the Info.plist file which is available in our projects. But you can use this approach to load up the content of any property file you create in your iOS app Xcode project.

  • Use Bundle.main.path to determine the path to an info.plist property file,
  • Create NSDictionary with a content of a property file,
  • Print out the value of one of the Info.plist property file keys.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Info.plist. Load Content of a Property File into NSDictionary

import UIKit
class ViewController: UIViewController  {
override func viewDidLoad() {
    super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    var resourceFileDictionary: NSDictionary?
    
    //Load content of Info.plist into resourceFileDictionary dictionary
    if let path = Bundle.main.path(forResource: "Info", ofType: "plist") {
        resourceFileDictionary = NSDictionary(contentsOfFile: path)
    }
    
    if let resourceFileDictionaryContent = resourceFileDictionary {
        
        // Get something from our Info.plist like MinimumOSVersion
        print("MinimumOSVersion = \(resourceFileDictionaryContent.object(forKey: "MinimumOSVersion")!)")
        
        //Or we can print out entire Info.plist dictionary to preview its content
        print(resourceFileDictionaryContent)
        
    }
  }
}

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!