我收到错误,但我不明白为什么:
“由于信号导致命令失败:分段错误:11”
我的代码也会定期变白。 这是我的代码。在我的项目中,我调用一个 API,它工作得很好,我将该 JSON 信息传递到数据库,然后视图控制器将使用数据存储中的该信息来填充集合视图。
import Foundation
class MovieDataBase
{
static let sharedInstance = MovieDataBase()
private init() {}
//creating a singleton for the MovieDataStore
var movies = [Movie]()
//array to store all the movie objects from the json
let movieSearchTerms = ["love", "fantasy", "romance", "mystery", "thriller", "musical", "family", "horror", "sci-fi"]
func getMoviesWithCompletion(completion: () -> ()) {
let randomNum = arc4random_uniform(UInt32(movieSearchTerms.count))
OMDBAPIClient.getMovieResultsFromSearch(self.movieSearchTerms[Int(randomNum)]) { (arrayOfMovies) in
for singleMovie in arrayOfMovies
{
let neededTitle = singleMovie["title"] as? String
let neededYear = singleMovie["year"] as? String
let neededImbdID = singleMovie["imdbID"] as? String
let neededType = singleMovie["type"] as? String
let neededPosterURL = singleMovie["posterURL"] as? String
guard let
unwrappedTitle = neededTitle,
unwrappedYear = neededType,
unwrappedImbdID = neededImbdID,
unwrappedType = neededType,
unwrappedPosterURL = neededPosterURL
else { print("AN ERROR OCCURRED HERE"); return }
var movie = Movie.init(title: unwrappedTitle, year: singleMovie["year"], imdbID: singleMovie["imbdID"], type: singleMovie["type"], posterURL: singleMovie["posterURL"])
movies.append(movie)
}
completion()
}
}
}
它不起作用的原因是我从 JSON 中需要的信息拼写错误,因此它抛出了各种错误。下次明智的做法是查看 postman 中的 JSON 信息,并确保您编写的代码与您需要的 JSON 信息相匹配,否则您将遇到各种错误。