Swift: Creating Plist Programmatically

It’s practical to use plist in files for iOS projects either to store configuration settings or to store any other information you may need in the execution of your application.

The plist or property list structure is key-value relation:

example:

plist

Before we try to generate the plist file we can check if the plist exists:


let fileManager = FileManager.default
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let path = documentDirectory.appending("/example.plist")

In case the file doesn’t exist:

    if (!fileManager.fileExists(atPath: path)) {
            /*
                        here is where we are going to create the plist file
            */
    }

To generate the plist file lets start with the content. In this case we are going to use the capitals of the states:

let dicContent:[String: String] = ["Alabama": "Montgomery", "Alaska":"Juneau","Arizona":"Phoenix"]

Now we are going to convert the dictionary content to NSDictionary:

let plistContent = NSDictionary(dictionary: dicContent)

With the content already in NSDictionary we can try to write to disk:

let success:Bool = plistContent.write(toFile: path, atomically: true)

We can check if write to disk was successful

 
if success {
	print("file has been created!")
}else{
	print("unable to create the file")
}
	 func pListCreation() {
           let fileManager = FileManager.default
            let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
            let path = documentDirectory.appending("/example.plist")
           
            if (!fileManager.fileExists(atPath: path)) {
                let dicContent:[String: String] = ["Alabama": "Montgomery", "Alaska":"Juneau","Arizona":"Phoenix"]
                let plistContent = NSDictionary(dictionary: dicContent)
                let success:Bool = plistContent.write(toFile: path, atomically: true)
                if success {
                    print("file has been created!")
                }else{
                    print("unable to create the file")
                }
               
            }else{
                print("file already exist")
            }
        }

Please let me know if you have any questions by leaving a comment below or on twitter @luisemedr

Swift Fundamentals : Constants and Variables

In Swift there are two ways of storing data, one is using variables and the other is constants.

Variables

Swift Variables can be modified after you assign an initial value, but you can not change their type. The reason being, the variables are mutable. When you declare a variable they can infer the variable type.
For example:

var car = “vw”

the variable car is infered to be a string. Because the car is mutable the value can change:

car = “bmw”

After variables are defined you can not change their type:

car = 2016 // it would give an error

Constants

Swift Constants are inmutable and they infer their type when you initialize the constant. For example:

let year = 2016

year is inferred to be Int., year can not be modified either in the value or the type.

Static Typing and Type Inference

When you create a variable or a constant they can be a strong type or use type inference to determine the initial type.

Statically Typed

Being statically typed means that all your variables and constants MUST have their types declared in advance. Once you have declare (inferred) your variables and constants type cannot be change.

var book:String
book = "Swift book"

Type Inference / Strongly Typed

You can explicitly declare the type of your variable/constant but you don’t need to do this in Swift, it will infer the type if you assign the initial value. For example this is how you explicitly declare its type:

var message: String
message = “greetings”

Another way of doing it would be

var message: String  = “greetings”

Please let me know if you have any questions by leaving a comment below or on twitter @luisemedr