Search code examples
iosswiftplist

Swift - Using created plist


I am creating an API Keys plist for my application and I am trying to figure out how to create a function that stores these plist values in variables that can be used in my other files. My ApiKeys.plist looks like this:

Root - Dictionary - (blank)

  • Parse_App_Id - String - xxxxxxx
  • Parse_Client_Id - String - xxxxxx

Then I have an ApiKeys.swift file where my function is stored:

import Foundation

func valueForAPIKey(#keyname:String) -> String {

    let filePath = NSBundle.mainBundle().pathForResource("ApiKeys", ofType: "plist")

    let plist = NSDictionary(contentsOfFile: filePath!)

    let parseAppId:String = plist?.objectForKey("Parse_App_Id") as String
    let parseClientId:String = plist?.objectForKey("Parse_Client_Id") as String


    return parseAppId && parseClientID

}

Side note: I used a tutorial to build this function and I'm curious if this is the best way to set up a function to store two plist keys.

First error comes on the return line. Can't invoke && argument list of type '(String, String)'

AppDelegate.swift:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.


            Parse.setApplicationId(valueForAPIKey.(keyname: "ParseAppId")), clientKey: valueForAPIKey.(keyname:"ParseClientId"))
            PFFacebookUtils.initializeFacebook()

        return true
    }

Second Error: Expected ',' separator at valueForAPIKey.(keyname: "ParseAppID")

I'm very new to the plist concepts so please excuse my clear errors and misdirection


Solution

  • There is some confusion I think, not only with the plist thing.

    First, your method expect to return a String.

    Either your return your results as a single String :

    func valueForAPIKey(#keyname:String) -> String {
        let filePath = NSBundle.mainBundle().pathForResource("ApiKeys", ofType: "plist")
    
        let plist = NSDictionary(contentsOfFile: filePath!)
    
        let parseAppId:String = plist?.objectForKey("Parse_App_Id") as String
        let parseClientId:String = plist?.objectForKey("Parse_Client_Id") as String
    
        return "\(parseAppId) \(parseClientID)"
    }
    

    Or you choose to return a tuple (or an array), and you'll access the element 0 or 1 depending on which one you need.

    func valueForAPIKey(#keyname:String) -> (String, String) {
        let filePath = NSBundle.mainBundle().pathForResource("ApiKeys", ofType: "plist")
    
        let plist = NSDictionary(contentsOfFile: filePath!)
    
        let parseAppId:String = plist?.objectForKey("Parse_App_Id") as String
        let parseClientId:String = plist?.objectForKey("Parse_Client_Id") as String
    
        return (parseAppId, parseClientID)
    }
    

    However, since you are using a parameter keyname, why don't you use it ?

    func valueForAPIKey(#keyname:String) -> String {
        let filePath = NSBundle.mainBundle().pathForResource("ApiKeys", ofType: "plist")
    
        let plist = NSDictionary(contentsOfFile: filePath!)
    
        let id : String = plist?.objectForKey(keyname) as String
    
        return id
    }
    

    Then you'll be able to use it like so :

    Parse.setApplicationId(valueForAPIKey("Parse_App_Id"), clientKey : valueForAPIKey("Parse_Client_Id"))