[Solved] Variable keeps returning nil – Swift iOS


All your suggestions were valid but this is what actually worked.

import Foundation
import Alamofire

class GreetingObjectHandler {
    var greetings: [Greeting] = []

    init(filename: String) {

        let fileP = NSURL(string: "http://localhost:2403/users/me")
        let jsonD = NSData(contentsOfURL:fileP!)
        let jso = JSON(data: jsonD!, options: NSJSONReadingOptions.AllowFragments, error: nil)
        var id = jso["id"]


        let filePath = NSURL(string: "http://localhost:2403/users")
        let jsonData = NSData(contentsOfURL:filePath!)
        let json = JSON(data: jsonData!, options: NSJSONReadingOptions.AllowFragments, error: nil)

        for (key: String, subJson: JSON) in json {

            var language:String?, link: String?, description:String?, greetingText: String?

            for (key1, value:JSON) in subJson {
                switch key1 {
                case "displayName": language = value.string
                case "id": link = value.string
                case "username": description = value.string
                case "mainSkill": greetingText = value.string
                default: break
                }
            }

            let greeting = Greeting(language: language, link: link, description: description, greetingText: greetingText)
            self.greetings.append(greeting)
            self.greetings = self.greetings.filter { $0.link != "\(id)"}
        }
    }

    func getGreetingsAsAnyObjects() -> [String: [AnyObject]]{

        return [Constant.GreetingOBJHandlerSectionKey: greetings.map { $0 as AnyObject }]
    }
}

solved Variable keeps returning nil – Swift iOS