0
votes

sorry in advance for my bad english. I have a problem with my Swiftcode, i'm new in Swift so maybe you can help me :)

Here is my Code.

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
        data, response, error in

        if(error != nil)
        {
            println("error\(error)")
            return;
        }

        var err: NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary

        if let parseJSON = json
        {
            var resultValue:String = parseJSON["message"] as String!;
            println("result: \(resultValue)")
            self.LabelFalscheEingabe.text = "\(resultValue)";

            if(resultValue == "Success")
            {
             var Projects:Array = parseJSON["projects"] as Array!; // here is the Error
            }
        }
        task.resume()
    }

'projects' is a variable from type Array on the server, so i want to get it as Array from the server, but if I try this i get the following error.

Error: "Type 'String' does not conform to protocol 'NSCopying'".

Thanks in advance :)

1
Your english looks fine to me, no need to apologise.Aphire

1 Answers

2
votes

YourProjects array can't be declared like that, Swift has to know the type of the objects in the array.

If you don't know the type, then make it an array of AnyObject:

if let Projects = parseJSON["projects"] as? [AnyObject] {
    // do something with Projects
}

If you know it's an array of Strings, for example:

if let Projects = parseJSON["projects"] as? [String] {
    // do something with Projects
}

An array of Integers:

if let Projects = parseJSON["projects"] as? [Int] {
    // do something with Projects
}

An array of dictionaries made from JSON:

if let Projects = parseJSON["projects"] as? [[String:AnyObject]] {
    // do something with Projects
}

Etc.