Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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
Ios 根据Int查找JSON字典中的前10项_Ios_Arrays_Json_Swift - Fatal编程技术网

Ios 根据Int查找JSON字典中的前10项

Ios 根据Int查找JSON字典中的前10项,ios,arrays,json,swift,Ios,Arrays,Json,Swift,我正在寻找JSON字典的前10个实例,其中Int值最高 所以,就我展示的这个例子而言,我正在根据受欢迎程度排名,寻找排名前十的电影。我在下面贴了一个字典的例子 词典的一部分: { "cast": [ { "id": 201, "character": "Praetor Shinzon", "original_title": "Star Trek: Nemesis", "overview": "En route to the honeymoon of William Riker t

我正在寻找JSON字典的前10个实例,其中Int值最高

所以,就我展示的这个例子而言,我正在根据受欢迎程度排名,寻找排名前十的电影。我在下面贴了一个字典的例子

词典的一部分:

{
"cast": [
{
  "id": 201,
  "character": "Praetor Shinzon",
  "original_title": "Star Trek: Nemesis",
  "overview": "En route to the honeymoon of William Riker to Deanna Troi on her home planet of Betazed, Captain Jean-Luc Picard and the crew of the U.S.S. Enterprise receives word from Starfleet that a coup has resulted in the installation of a new Romulan political leader, Shinzon, who claims to seek peace with the human-backed United Federation of Planets. Once in enemy territory, the captain and his crew make a startling discovery: Shinzon is human, a slave from the Romulan sister planet of Remus, and has a secret, shocking relationship to Picard himself.",
  "vote_count": 643,
  "video": false,
  "media_type": "movie",
  "release_date": "2002-12-13",
  "vote_average": 6.2,
  "title": "Star Trek: Nemesis",
  "popularity": 7.61,
  "original_language": "en",
  "genre_ids": [
    28,
    12,
    878,
    53
  ],
  "backdrop_path": "/1SLR0LqYPU3ahXyPK9RZISjI3B7.jpg",
  "adult": false,
  "poster_path": "/n4TpLWPi062AofIq4kwmaPNBSvA.jpg",
  "credit_id": "52fe4226c3a36847f8007d05"
},
{
  "id": 855,
  "character": "Spec. Lance Twombly",
  "original_title": "Black Hawk Down",
  "overview": "When U.S. Rangers and an elite Delta Force team attempt to kidnap two underlings of a Somali warlord, their Black Hawk helicopters are shot down, and the Americans suffer heavy casualties, facing intense fighting from the militia on the ground.",
  "vote_count": 2540,
  "video": false,
  "media_type": "movie",
  "release_date": "2001-12-28",
  "vote_average": 7.3,
  "title": "Black Hawk Down",
  "popularity": 11.504,
  "original_language": "en",
  "genre_ids": [
    28,
    36,
    10752
  ],
  "backdrop_path": "/7u2p0VxnhVMHzfSnxiwz5iD3EP7.jpg",
  "adult": false,
  "poster_path": "/yUzQ4r3q1Dy0bUAkMvUIwf0rPpR.jpg",
  "credit_id": "52fe4282c3a36847f80248ef"
},
从这本词典中,根据受欢迎程度排名前十的电影,正确的代码是什么

下面是一些代码:

 struct Cast: Codable {
    let title: String
    let character: String
    let poster_path: String?
    let id: Int
    let popularity: Double?
}

var filmCredits = [Cast]()
我遇到的第一个问题是当我使用
return10
返回10个结果时:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}
在调用my
cellForItemAt
func中的indexPath时,我收到错误
Thread 1:致命错误:索引超出范围

以下是JSON解码器函数:

func loadFilms() {

    let apiKey = ""
    let url = URL(string: "https://api.themoviedb.org/3/person/\(id)/combined_credits?api_key=\(apiKey)&language=en-US")
    let request = URLRequest(
        url: url! as URL,
        cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData,
        timeoutInterval: 10 )

    let session = URLSession (
        configuration: URLSessionConfiguration.default,
        delegate: nil,
        delegateQueue: OperationQueue.main
    )

    let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
        if let data = data {
            do {
                let films = try! JSONDecoder().decode(Credits.self, from: data)
                self.filmCredits = films.cast!
                self.topCollection.reloadData()

            }

        }

        self.topCollection.reloadData()

    })


    task.resume()

}
我最不确定的是如何只选出排名前十的电影。我会使用类似于
过滤器
映射
的东西吗?

您需要

filmCredits.sort{ $0.popularity > $1.popularity}


首先,在
Credits
中声明
cast
非可选

struct Credits: Decodable {
    let cast: [Cast]
}
分配给数据源数组时,按
流行度
对数组进行降序排序

self.filmCredits = films.cast.sorted{($0.popularity ?? 0.0) > $1.popularity ?? 0.0})
不要硬编码章节中的NumberOfItems。如果
cast
数组包含少于10项,则会发生崩溃。添加一个条件,如果项目数大于10,则显示10个项目,否则显示数组中的项目数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let numberOfCredits = filmCredits.count
    return numberOfCredits > 10 ? 10 : numberOfCredits
}
捕获一个可能的错误并在主线程上重新加载集合视图

if let data = data {
    do {
        let films = try JSONDecoder().decode(Credits.self, from: data)
        self.filmCredits = films.cast.sorted{($0.popularity ?? 0.0) > $1.popularity ?? 0.0})
    } catch{ print(error) }
}
DispatchQueue.main.async {
    self.topCollection.reloadData()
}

不能在委托方法
collectionView(\ucollectionview:UICollectionView,numberofitemsinssection:Int)->Int中写入
return 10
。你不能确定你总是会有10个结果。如果少于10,应用程序将在
itemForRowAt
中崩溃

注意:您提到的
cellForRowAt
是用于
UITableView
s的,但是您的代码显示了
collectionView
,请确保使用了正确的委托方法

然后填充一个包含人气信息的数组,按降序排序,您就可以使用上面提到的内容:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return min(filmCredits.count,10)
    }

到目前为止,您尝试了什么?@JSharpp,以及上面的代码,请不要忘记在函数session.dataTask(with:request,completionHandler:())中的DispatchQueue.main.async{}内运行self.topCollection.reloadData()。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return min(filmCredits.count,10)
    }