我正在尝试将网页数据放入桌面视图和Swift中的自定义单元格中。
我的应用程序死于
if let tmpdata = response.data {
for entry in tmpdata.valueForKey("results") as! [NSDictionary] {
let musicTrack = MusicTrack(data: entry)
self.musicTracks.append(musicTrack)
}
Heres我的音乐管理器的代码。
import Foundation
import Alamofire
let musicUrl = "https://itunes.apple.com/search?term=one%20republic"
class MusicManager {
var musicTracks: [MusicTrack] = []
let session = NSURLSession.sharedSession()
func getMusicTracks(onComplete : (results :[MusicTrack]) -> Void) {
Alamofire.request(.GET, musicUrl).responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
if let tmpdata = response.data {
for entry in tmpdata.valueForKey("results") as! [NSDictionary] {
let musicTrack = MusicTrack(data: entry)
self.musicTracks.append(musicTrack)
}
onComplete(results: self.musicTracks)
}
}
}
}
}
然后是我的ListViewController
import UIKit
class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let musicManager = MusicManager()
var musicTracks: [MusicTrack]!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
musicManager.getMusicTracks { (results) -> Void in
self.musicTracks = results
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.musicTracks != nil){
return self.musicTracks.count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: MusicCell = self.tableView.dequeueReusableCellWithIdentifier("musicCell") as! MusicCell
cell.posterImage.image = nil
let mTrack = self.musicTracks[indexPath.row]
let imagedata = NSData(contentsOfURL: NSURL(string: mTrack.thumbnail)!)
if let tmpdata = imagedata {
cell.posterImage.image = UIImage(data: tmpdata)
}
cell.artistName.text = mTrack.artistName
cell.trackName.text = mTrack.trackName
return cell
}
}
我得到的错误是
***因未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]: 这个类不是关键结果的关键值编码兼容。'
任何帮助或方向都会很棒。
谢谢!
答案 0 :(得分:0)
I faced the similar problem to but i came up with a solution.
-> Add the below function to yourviewcontroller.swift file
func postWebserviceWithURL(strUrl: String, param: NSDictionary?, completionHandler: (NSDictionary?, NSError?) -> ()) -> (){
Alamofire.request(.POST, strUrl, parameters: param as? [String : AnyObject], encoding: ParameterEncoding.URL).responseJSON { response in
switch response.result {
case .Success(let data):
let json = data as? NSDictionary
completionHandler(json, nil)
case .Failure(let error):
completionHandler(nil, error)
}
}
}
-> then for calling the func/api, use below code
self.postWebserviceWithURL("passyourURL", param: parameters) { (response, error) -> () in
if error == nil{
print("success")
}
else {
print("failed")
}
}
else{
print("error")
}
}