[英]Alamofire Request Not Being Called in Closure
I am having trouble getting my Alamofire call to execute my json Closure in Swift. 我无法让Alamofire调用在Swift中执行json闭包。 Below is my NetworkingService file: 以下是我的NetworkingService文件:
import Foundation
import Alamofire
import SwiftyJSON
class NetworkingService {
static let shared = NetworkingService()
private init() {}
func getSong(completionHandler: @escaping (JSON) -> Void) {
Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
print(response)
}
}
}
Below is my Model class, Song: 以下是我的模型类Song:
import Foundation
import SwiftyJSON
struct Song {
let trackName: String
let trackArtist: String
let trackFile: String
// failable intializer
init?(json: JSON) {
guard let trackName = json["track"].string,
let trackArtist = json["artist"].string,
let trackFile = json["file"].string
else { return nil }
self.trackName = trackName
self.trackArtist = trackArtist
self.trackFile = trackFile
}
}
Below is the relevant code in my HomeController file: 以下是我的HomeController文件中的相关代码:
import UIKit
import SwiftyJSON
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
showJSON()
}
var songs = [SongTest]()
func showJSON() {
print("This print outs")
NetworkingService.shared.getSong { (json) in
print("This does not print out")
self.songs = []
if let listOfSongs = json["songs"].array {
for item in listOfSongs {
let song = SongTest(json: item)
self.songs.append(song!)
}
for r in self.songs {
print(r.trackName)
print(r.trackArtist)
print(r.trackFile)
}
}
}
}
}
In my HomeController, I am able to see ("this prints out") in my console along with the JSON data from the url, which is managed in my NetworkService file. 在我的HomeController中,我可以在控制台中看到(“打印出来的内容”)以及URL中的JSON数据,该数据在NetworkService文件中进行管理。 I have set a breakpoint at NetworkingService.shared.getSong { (json) in
. 我在中的NetworkingService.shared.getSong { (json) in
设置了一个断点。 If the JSON data is printing out, it makes me believe the error is occurring with this closure: (json)
. 如果正在打印JSON数据,则使我相信此闭包(json)
发生了错误。 Any ideas on why this is happening and how to fix-it? 关于为什么发生这种情况以及如何解决的任何想法?
In your first method you define a completionHandler
that you never call : This should works: 在您的第一个方法中,定义一个从未调用过的completionHandler
程序:这应该起作用:
func getSong(completionHandler: @escaping (JSON) -> Void) {
Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
print(response)
completionHandler(response.result.value)
}
}
I was able to solve my problem: 我能够解决我的问题:
class NetworkingService {
static let shared = NetworkingService()
private init() {}
func getSong(completionHandler: @escaping (JSON) -> Void) {
Alamofire.request("http://10.0.1.7/myiosapp/v1/songs.php", method: .get, parameters: nil, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
guard let value = response.result.value else {
completionHandler(response.result.error as! JSON)
return
}
let swiftyJsonVar = JSON(value)
completionHandler(swiftyJsonVar)
}
}
}
按照此代码,我认为您错过了调用completionHandler(response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.