Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图从segue(swift-iOS)获取视图时应用程序崩溃_Ios_Swift - Fatal编程技术网

试图从segue(swift-iOS)获取视图时应用程序崩溃

试图从segue(swift-iOS)获取视图时应用程序崩溃,ios,swift,Ios,Swift,所以,我正在构建一个应用程序,使用OMDB API从IMDB搜索电影。有两种方法可以根据返回的电影标题进行搜索:海报、标题、类型、年份、imdbID。另一种方法是通过imdbTitle搜索,它只返回一部电影,其中包含更多的内容,如演员、导演等。我的搜索工作正常,当我在搜索栏上键入电影标题时,我会得到一个电影列表。但是,当我点击一部电影以显示包含电影详细信息的视图时,我的应用程序崩溃了。我在代码上进行了一些打印,以查看它运行了多远,当我尝试执行getMovieByID函数时,它崩溃了 这就是我想做

所以,我正在构建一个应用程序,使用OMDB API从IMDB搜索电影。有两种方法可以根据返回的电影标题进行搜索:海报、标题、类型、年份、imdbID。另一种方法是通过imdbTitle搜索,它只返回一部电影,其中包含更多的内容,如演员、导演等。我的搜索工作正常,当我在搜索栏上键入电影标题时,我会得到一个电影列表。但是,当我点击一部电影以显示包含电影详细信息的视图时,我的应用程序崩溃了。我在代码上进行了一些打印,以查看它运行了多远,当我尝试执行getMovieByID函数时,它崩溃了

这就是我想做的

使用Alamofire在OMDB API上通过id获取电影:

func getMovieByIdJSON(imdbID: String, completionHandler: @escaping (Dictionary<String, String>) -> Void) {

let url = "https://www.omdbapi.com/?i=\(imdbID)"

Alamofire.request(url).responseJSON {
    response in

    if let json = response.result.value {
        completionHandler((json as? Dictionary<String, String>)!)
    } 
  }

}
我的电影要通过ID获取电影:

class MovieDAO {

func getMovieByID(imdbID: String, completionHandler: @escaping (Movie) -> ()) {

    getMovieByIdJSON(imdbID: imdbID, completionHandler: {
        (dict) in

        let movie: Movie = Movie(poster: dict["Poster"]!, title: dict["Title"]!, runtime: dict["Runtime"]!, director: dict["Director"]!, actors: dict["Actors"]!, genre: dict["Genre"]!, plot: dict["Plot"]!, production: dict["Production"]!, year: dict["Year"]!, imdbID: dict["imdbID"]!, imdbRating: dict["imdbRating"]!)

        completionHandler(movie)
    })
  }

}
最后,可能导致这一混乱的代码

在我的SearchTableViewController上,我有以下步骤:

let searchSegue = "segueFromSearch"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destination = segue.destination as? MovieViewController
    let searchIndex = tableView.indexPathForSelectedRow?.row
    let selectedImdbID = self.movies[searchIndex!].imdbID

    print("IMDB_ID: \(String(describing: selectedImdbID))")

    if segue.identifier == searchSegue {
        destination?.movieImdbID = selectedImdbID!
        print("Debug print: I get til here")
    }
}
显示电影详细信息的视图具有以下MovieViewController:

import UIKit

class MovieViewController: UIViewController {

let DAO: MovieDAO = MovieDAO()
var movie: Movie? = nil
var movieImdbID: String?


override func viewDidLoad() {
    super.viewDidLoad()

    print("Movie Controller view didLoad - before get the movie function")

    DAO.getMovieByID(imdbID: self.movieImdbID!, completionHandler: {
        movieResult in

        print("Imdb self.movie: \(String(describing: self.movie))")
        self.movie = movieResult

        print("after self.movie")

    }) 
  }
}
我模拟了一次观看电影细节的尝试,我的控制台输出如下:

2017-04-25 09:29:17.016079-0300 Favorite Movies[14492:460113] [MC] Reading from private effective user settings.
IMDB_ID: Optional("tt0437236")
Debug print: I get til here
Movie Controller view didLoad - before get the movie function
(lldb) 
所以,我想问题在于我的MovieViewController中的这个函数,因为应用程序在尝试执行它时会崩溃。知道我做错了什么吗?我错过了什么

注:很抱歉有这么多代码,但我试图解释并显示崩溃前的所有路径,因为问题可能不在我想的地方

更新:## xcode显示以下内容: [![xcode][1]][1]

更新2 正如@unkg所问,这里是我的getMovieByIdJSON中的第13行:


注:我注意到我的segue函数正在通过(如控制台上打印的)“可选(“tt0944849”)”。是这样吗?此可选打印也作为字符串的一部分传递

看一个omdbapi响应示例,我认为您无法可靠地转换到
字典。注意
评级:[]
是一个空
数组
。您可能需要转换到
字典
。我也会小心拆开包裹的力量

如果打印是可选的,请确保打开
选择edimdbid


顺便说一句,我是一个超级粉丝。有了它,你可以把你的电影课变成这样

class Movie: Mappable {
    var poster, title, runtime, director, actors, genre, plot, production, year, imdbID, imdbRating : String?

    required init?(map: Map) {
    }

    func mapping(map: Map) {
        poster    <- map["Poster"]
        title     <- map["Title"]
        runtime   <- map["Runtime"]
        director  <- map["Director"]
        //...etc...
    }
}
类电影:可映射{
var海报、标题、运行时、导演、演员、流派、情节、制作、年份、imdbID、imdbRating:String?
必需的初始化?(映射:映射){
}
func映射(映射:映射){

海报错误消息是什么?是否有崩溃日志?您确定
segue.destination
是您的MovieViewController吗?我建议在
prepare(segue:)中设置断点
并检查变量是否存在nil@ChanJingHong它不是零,正如您所看到的,在我的segue prepare函数中,它在控制台上打印电影ID。我的意思是,在这一点上有一个电影ID。但我不知道为什么当我调用函数getMovieByID从prepare segue接收这个ID时,应用程序崩溃。@Sweeper我更新了@dmorrow响应的错误图像为+1。您的崩溃与segue或任何iOS系统调用无关。您的屏幕截图不是决定性的,但JSON反序列化是失败的。您可以使用ObjectMapper作为库依赖项,静态或通过依赖项管理器进行更新。或者您可以使用代码生成工具,如AhmedAli的JSON导出(要求您将他的解决方案作为Mac OS应用程序在XCode中构建)嘿,@dmorrow,谢谢!我按照您的建议尝试这样做,但我有一些疑问和另一个错误。您能帮我解决吗?这里的问题:
{
    "Title": "Kingsman: The Golden Circle",
    "Year": "2017",
    "Rated": "N/A",
    "Released": "29 Sep 2017",
    "Runtime": "N/A",
    "Genre": "Action, Adventure, Comedy",
    "Director": "Matthew Vaughn",
    "Writer": "Jane Goldman (screenplay), Matthew Vaughn (screenplay), Mark Millar (characters), Dave Gibbons (characters)",
    "Actors": "Taron Egerton, Channing Tatum, Julianne Moore, Pedro Pascal",
    "Plot": "When their headquarters are destroyed and the world is held hostage, the Kingsman's journey leads them to the discovery of an allied spy organization in the US. These two elite secret organizations must band together to defeat a common enemy.",
    "Language": "English",
    "Country": "UK, USA",
    "Awards": "N/A",
    "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BNTBlOWZhZTctOTY0MC00Y2QyLTljMmYtZDkxZDFlMWU4Y2EyXkEyXkFqcGdeQXVyNDg2MjUxNjM@._V1_SX300.jpg",
    "Ratings": [],
    "Metascore": "N/A",
    "imdbRating": "N/A",
    "imdbVotes": "N/A",
    "imdbID": "tt4649466",
    "Type": "movie",
    "DVD": "N/A",
    "BoxOffice": "N/A",
    "Production": "20th Century Fox",
    "Website": "http://www.foxmovies.com/movies/kingsman-the-golden-circle",
    "Response": "True"
}
class Movie: Mappable {
    var poster, title, runtime, director, actors, genre, plot, production, year, imdbID, imdbRating : String?

    required init?(map: Map) {
    }

    func mapping(map: Map) {
        poster    <- map["Poster"]
        title     <- map["Title"]
        runtime   <- map["Runtime"]
        director  <- map["Director"]
        //...etc...
    }
}