问题描述
我正在努力让 AlamoFire 在使用 SwiftyJSON 取出“结果”JSON 数组对象的函数之前触发。我发现在函数 getWatchList 中,在 AF.request 完成之前返回了 listObject。这会影响 getobject 函数中的代码,因为它当然会被赋予一个空的 JSON 对象。我使用的 API 是 The Movie DB。
请注意,到目前为止,这些都是在 Xcode 上的 Playground 中编写的,所以这些有点粗糙,而且很久没有使用 AlamoFire 或 SwiftyJSON!
我曾尝试在此使用 dispatch Group,但似乎无法使其正常工作(我已删除此代码,因为它没有任何区别)
请有人建议在这里做什么?
func getWatchList() -> JSON {
let url = "https://api.themoviedb.org/3/account/\(accountId)/watchlist/movies?api_key=\(api)&session_id=\(sessionId)&sort_by=created_at.asc&page=1"
var listObject = JSON()
AF.request(url,method: .get).responseJSON { (responce) in
switch responce.result {
case .success(let value):
let json = JSON(value)
//print(json)
listObject = json
print(listObject)
case .failure(let error):
print(error)
}
}
print(listObject)
return listObject
} // end of getWatchList
func getPoster() {
} // end of getPoster
// MARK: func getobject
// to use this command,state a int for the positionNumber of the object you would like
// if you would like a random item,enter 99999
func getobject(positionNumber: Int) {
var listObject = JSON()
if positionNumber != 99999 {
listObject = getWatchList()
if listObject != .null {
let resultsObject = listObject["results"].arrayValue
print(resultsObject)
print(listObject)
} else {
print("JSON listObject is empty")
}
} else {
print("findiong random position")
}
} // end of getobject
getobject(positionNumber: 1)
目前,当我调用 getobject 函数时,我得到以下结果:
{
}
[]
{
}
{
"page" : 1,"total_pages" : 1,"total_results" : 20,"results" : [
{
"popularity" : 100.55800000000001,"video" : false,"poster_path" : "\/78lPtwv72eTNqFW9COBYI0dWDJa.jpg","Vote_count" : 20546,"release_date" : "2008-04-30","id" : 1726,"backdrop_path" : "\/vbY95t58MDArtyUXUIb8Fx1dCry.jpg","overview" : "After being held captive in an Afghan cave,billionaire engineer Tony Stark creates a unique weaponized suit of armor to fight evil.","Vote_average" : 7.5999999999999996,"title" : "Iron Man","adult" : false,"genre_ids" : [
28,878,12
],"original_language" : "en","original_title" : "Iron Man"
},{
"popularity" : 82.622,"poster_path" : "\/6WBeq4fCfn7AN0o21W9qNcRF2l9.jpg","Vote_count" : 16085,"release_date" : "2010-04-28","id" : 10138,"backdrop_path" : "\/hCVQPB77eXBGh9abVdrWtKpKgkN.jpg","Vote_average" : 6.7999999999999998,"overview" : "With the world Now aware of his dual life as the armored superhero Iron Man,billionaire inventor Tony Stark faces pressure from the government,the press and the public to share his technology with the military. Unwilling to let go of his invention,Stark,with Pepper Potts and James 'Rhodey' Rhodes at his side,must forge new alliances – and confront powerful enemies.","title" : "Iron Man 2","genre_ids" : [
12,28,878
],"original_title" : "Iron Man 2"
},{
"popularity" : 76.784999999999997,"poster_path" : "\/prSfAi1xGrhLQNxVSUFh61xQ4Qy.jpg","Vote_count" : 16447,"release_date" : "2011-04-21","id" : 10195,"backdrop_path" : "\/cDJ61O1STtbWNBwefuqVrRe3d7l.jpg","overview" : "Against his father Odin's will,The Mighty Thor - a powerful but arrogant warrior god - recklessly reignites an ancient war. Thor is cast down to Earth and forced to live among humans as punishment. Once here,Thor learns what it takes to be a true hero when the most dangerous villain of his world sends the darkest forces of Asgard to invade Earth.","title" : "Thor",14,28
],"original_title" : "Thor"
},{
"popularity" : 61.130000000000003,"poster_path" : "\/vSNxAJTlD0r02V9sPYpOjqDZXUK.jpg","Vote_count" : 16748,"release_date" : "2011-07-22","id" : 1771,"backdrop_path" : "\/yFuKvT4Vm3sKHdFY4eG6I4ldAnn.jpg","Vote_average" : 7,"overview" : "During World War II,Steve Rogers is a sickly man from brooklyn who's transformed into super-soldier Captain America to aid in the war effort. Rogers must stop the Red Skull – Adolf Hitler's ruthless head of weaponry,and the leader of an organization that intends to use a mysterIoUs device of untold powers for world domination.","title" : "Captain America: The First Avenger",12,"original_title" : "Captain America: The First Avenger"
},{
"popularity" : 84.697999999999993,"poster_path" : "\/qhPtAc1TKbMPqNvcdXSOn9Bn7hZ.jpg","Vote_count" : 17719,"release_date" : "2013-04-18","id" : 68721,"backdrop_path" : "\/tKa1gmGKAUVYnflYcadipeL3d9h.jpg","Vote_average" : 6.9000000000000004,"overview" : "When Tony Stark's world is torn apart by a formidable terrorist called the Mandarin,he starts an odyssey of rebuilding and retribution.","title" : "Iron Man 3","original_title" : "Iron Man 3"
},{
"popularity" : 168.13900000000001,"poster_path" : "\/RYMX2wcKCBAr24UyPD7xwmjaTn.jpg","Vote_count" : 24628,"id" : 24428,"release_date" : "2012-04-25","backdrop_path" : "\/nNmJRkg8wWnRmzQDe2FwKbPIsJV.jpg","Vote_average" : 7.7000000000000002,"overview" : "When an unexpected enemy emerges and threatens global safety and security,Nick Fury,director of the international peacekeeping agency kNown as S.H.I.E.L.D.,finds himself in need of a team to pull the world back from the brink of disaster. Spanning the globe,a daring recruitment effort begins!","title" : "The Avengers","genre_ids" : [
878,"original_title" : "The Avengers"
},{
"popularity" : 72.067999999999998,"poster_path" : "\/wp6OxE4poJ4G7c0U2ZIXasTSMR7.jpg","Vote_count" : 13386,"release_date" : "2013-10-29","id" : 76338,"backdrop_path" : "\/uhYoytlNaq46dG81wLmHqaSuzWu.jpg","Vote_average" : 6.5999999999999996,"overview" : "Thor fights to restore order across the cosmos… but an ancient race led by the vengeful Malekith returns to plunge the universe back into darkness. Faced with an enemy that even Odin and Asgard cannot withstand,Thor must embark on his most perilous and personal journey yet,one that will reunite him with Jane Foster and force him to sacrifice everything to save us all.","title" : "Thor: The Dark World",14
],"original_title" : "Thor: The Dark World"
},{
"popularity" : 57.707999999999998,"poster_path" : "\/5r1zWuzbsVAYSPzvCecAReGhN7k.jpg","Vote_count" : 14496,"release_date" : "2014-03-20","id" : 100402,"backdrop_path" : "\/yHB0eNR8rvCpn0VdghEwBsXAC0N.jpg","overview" : "After the cataclysmic events in New York with The Avengers,Steve Rogers,aka Captain America is living quietly in Washington,D.C. and trying to adjust to the modern world. But when a S.H.I.E.L.D. colleague comes under attack,Steve becomes embroiled in a web of intrigue that threatens to put the world at risk. Joining forces with the Black Widow,Captain America struggles to expose the ever-widening conspiracy while fighting off professional assassins sent to silence him at every turn. When the full scope of the villainous plot is revealed,Captain America and the Black Widow enlist the help of a new ally,the Falcon. However,they soon find themselves up against an unexpected and formidable enemy—the Winter Soldier.","title" : "Captain America: The Winter Soldier","original_title" : "Captain America: The Winter Soldier"
},{
"popularity" : 50.195,"poster_path" : "\/r7vmZjiyZw9rpJMQJdXpjgiCOk9.jpg","Vote_count" : 22422,"id" : 118340,"release_date" : "2014-07-30","backdrop_path" : "\/i34ijHY5hr52uYcKZ48wq7K6yUs.jpg","Vote_average" : 7.9000000000000004,"overview" : "Light years from Earth,26 years after being abducted,Peter quill finds himself the prime target of a manhunt after discovering an orb wanted by Ronan the Accuser.","title" : "Guardians of the galaxy","original_title" : "Guardians of the galaxy"
},{
"popularity" : 125.398,"poster_path" : "\/4ssDuvEDkSArWEdyBl2X5EHvYKU.jpg","Vote_count" : 17849,"release_date" : "2015-04-22","id" : 99861,"backdrop_path" : "\/xnqust9Li4oxfhXD5kcPi3UC8i4.jpg","Vote_average" : 7.2999999999999998,"overview" : "When Tony Stark tries to jumpstart a dormant peacekeeping program,things go awry and Earth’s Mightiest Heroes are put to the ultimate test as the fate of the planet hangs in the balance. As the villainous Ultron emerges,it is up to The Avengers to stop him from enacting his terrible plans,and soon uneasy alliances and unexpected action pave the way for an epic and unique global adventure.","title" : "Avengers: Age of Ultron","original_title" : "Avengers: Age of Ultron"
},{
"popularity" : 64.037999999999997,"poster_path" : "\/rQRnQfUl3kfp78nCWq8Ks04vnq1.jpg","Vote_count" : 15480,"release_date" : "2015-07-14","id" : 102899,"backdrop_path" : "\/7AyEEZVtFjNMCOEoz88pBqiAI8n.jpg","Vote_average" : 7.0999999999999996,"overview" : "Armed with the astonishing ability to shrink in scale but increase in strength,master thief Scott Lang must embrace his inner-hero and help his mentor,Doctor Hank Pym,protect the secret behind his spectacular Ant-Man suit from a new generation of towering threats. Against seemingly insurmountable obstacles,Pym and Lang must plan and pull off a heist that will save the world.","title" : "Ant-Man","original_title" : "Ant-Man"
},{
"popularity" : 143.08699999999999,"poster_path" : "\/rAGiXaUfPzY7CDEyNKUofk3Kw2e.jpg","Vote_count" : 17977,"release_date" : "2016-04-27","id" : 271110,"backdrop_path" : "\/kvRT3GwcnqGHzPjXIVrVPhUix7Z.jpg","Vote_average" : 7.4000000000000004,"overview" : "Following the events of Age of Ultron,the collective governments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers,causing two factions to side with Iron Man or Captain America,which causes an epic battle between former allies.","title" : "Captain America: Civil War","original_title" : "Captain America: Civil War"
},{
"popularity" : 110.429,"poster_path" : "\/xf8PbyQcR5ucXErmZNzdKR0s8ya.jpg","Vote_count" : 16884,"release_date" : "2016-10-25","id" : 284052,"backdrop_path" : "\/aL53oMdZKZRJRH8txH07DLuleF9.jpg","overview" : "After his career is destroyed,a brilliant but arrogant surgeon gets a new lease on life when a sorcerer takes him under her wing and trains him to defend the world against evil.","title" : "Doctor Strange","original_title" : "Doctor Strange"
},{
"popularity" : 103.402,"poster_path" : "\/y4MBh0EjBlMuOzv9axM4qJlmhzz.jpg","Vote_count" : 16527,"release_date" : "2017-04-19","id" : 283995,"backdrop_path" : "\/aJn9XeesqsrSLKcHfHP4u5985hn.jpg","overview" : "The Guardians must fight to keep their newfound family together as they unravel the mysteries of Peter quill's true parentage.","title" : "Guardians of the galaxy Vol. 2",35
],"original_title" : "Guardians of the galaxy Vol. 2"
},{
"popularity" : 144.21799999999999,"poster_path" : "\/rzRwTcFvttcN1ZpX2xv4j3tSdJu.jpg","Vote_count" : 15831,"release_date" : "2017-10-25","id" : 284053,"backdrop_path" : "\/kaifm5ryEOwYg8mLbq8HkPuM1Fo.jpg","overview" : "Thor is imprisoned on the other side of the universe and finds himself in a race against time to get back to Asgard to stop Ragnarok,the destruction of his home-world and the end of Asgardian civilization,at the hands of a powerful new threat,the ruthless Hela.","title" : "Thor: Ragnarok",35,"original_title" : "Thor: Ragnarok"
},{
"popularity" : 139.77699999999999,"poster_path" : "\/uxzzxijgPIY7slzFvMotPv8wjKA.jpg","Vote_count" : 17286,"release_date" : "2018-02-13","id" : 284054,"backdrop_path" : "\/AlFqBwJnokrp9zWTXOUv7uhkaeq.jpg","overview" : "King T'Challa returns home to the reclusive,technologically advanced African nation of Wakanda to serve as his country's new leader. However,T'Challa soon finds that he is challenged for the throne by factions within his own country as well as without. Using powers reserved to Wakandan kings,T'Challa assumes the Black Panther mantle to join with ex-girlfriend Nakia,the queen-mother,his princess-kid sister,members of the Dora Milaje (the Wakandan 'special forces') and an American secret agent,to prevent Wakanda from being dragged into a world war.","title" : "Black Panther","original_title" : "Black Panther"
},{
"popularity" : 345.20999999999998,"poster_path" : "\/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg","Vote_count" : 21720,"release_date" : "2018-04-25","id" : 299536,"backdrop_path" : "\/lmZFxXgJE3vgrciwuDib0N8CfQo.jpg","Vote_average" : 8.3000000000000007,"overview" : "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle,a new danger has emerged from the cosmic shadows: thanos. A despot of intergalactic infamy,his goal is to collect all six Infinity Stones,artifacts of unimaginable power,and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.","title" : "Avengers: Infinity War","original_title" : "Avengers: Infinity War"
},{
"genre_ids" : [
28,"backdrop_path" : "\/xjFgtltOOuBtLdNedQH8M1TTrjc.jpg","popularity" : 81.040999999999997,"overview" : "Just when his time under house arrest is about to end,Scott Lang once again puts his freedom at risk to help Hope van Dyne and Dr. Hank Pym dive into the quantum realm and try to accomplish,against time and any chance of success,a very dangerous rescue mission.","original_title" : "Ant-Man and the wasp","Vote_count" : 9760,"release_date" : "2018-07-04","poster_path" : "\/eivQmS3wqzqnQWILHLc4FsEfcXP.jpg","title" : "Ant-Man and the wasp","id" : 363088
},"backdrop_path" : "\/w2PMyoyLU22YvrGK3smVM9fW1jj.jpg","overview" : "The story follows Carol Danvers as she becomes one of the universe’s most powerful heroes when Earth is caught in the middle of a galactic war between two alien races. Set in the 1990s,Captain Marvel is an all-new adventure from a prevIoUsly unseen period in the history of the Marvel Cinematic Universe.","popularity" : 215.37899999999999,"original_title" : "Captain Marvel","Vote_count" : 11319,"release_date" : "2019-03-06","poster_path" : "\/AtsgWhDnHTq68L0lLsUrCnM7TjG.jpg","title" : "Captain Marvel","id" : 299537
},{
"genre_ids" : [
12,"backdrop_path" : "\/7RyHsO4yDXtBv1zUU3mTpHeQ0d5.jpg","overview" : "After the devastating events of Avengers: Infinity War,the universe is in ruins due to the efforts of the Mad Titan,thanos. With the help of remaining allies,the Avengers must assemble once more in order to undo thanos' actions and restore order to the universe once and for all,no matter what consequences may be in store.","popularity" : 298.50700000000001,"original_title" : "Avengers: Endgame","Vote_count" : 18056,"release_date" : "2019-04-24","poster_path" : "\/or06FN3Dka5tukK1e9sl16pB3iy.jpg","title" : "Avengers: Endgame","id" : 299534
}
]
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)