遵循iOS Swift教程但收到错误

时间:2015-05-29 23:29:56

标签: ios xcode swift xcode6

我正在关注视频教程,但我完全遵循了代码,但是我收到了教程中没有收到的错误。

非常感谢任何帮助。

//
//  ViewController.swift
//  IMDb Search
//
//  Created by James on 29/05/2015.
//  Copyright (c) 2015 James. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

@IBOutlet var titleLabel        : UILabel
@IBOutlet var releasedLabel     : UILabel
@IBOutlet var ratingLabel       : UILabel
@IBOutlet var plotLabel         : UILabel
@IBOutlet var posterImageView   : UIImageView


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func buttonPressed(sender: UIButton)
{
    self.searchIMDb("King of Kong")
}

func searchIMDb(forContent: String)
{

    var spacelessString = forContent.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    var urlPath - NSURL(string: "http://www.omdbapi.com/?t=\(spacelessString)")

    var session - NSURLSessio.sharedSession()

    var task = session.dataTaskWithURL(urlPath) {

        data, response, error -> Void in

        if (error) {

            println(error.localizedDescription)
        }

        var jsonError : NSError?

        var jsonResult - NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary<String, String>

        if (jsonError?) {

            println(jsonError!.localizedDescription)
        }

        self.titleLabel.text        = jsonResult["Title"]
        self.releasedLabel.text     = jsonResult["Released"]
        self.ratingLabel.text       = jsonResult["Rated"]
        self.plotLabel.text         = jsonResult["Plot"]
    }

    task.resume()

}

}

以下是我收到的错误。

Error Messages

修订后出现的新错误

New Error

最后的新错误

New Error

New App Delegate Error

新错误信息第一页

enter image description here

1 个答案:

答案 0 :(得分:0)

以消除错误中的错误:

if let error = error {

    println(error.localizedDescription)
}

json错误试试这样:

var jsonError : NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as [String:String] {
    self.titleLabel.text        = jsonResult["Title"]
    self.releasedLabel.text     = jsonResult["Released"]
    self.ratingLabel.text       = jsonResult["Rated"]
    self.plotLabel.text         = jsonResult["Plot"]
} else if let jsonError = jsonError {

        println(jsonError.localizedDescription)
}