大家好,我已经被困在这个问题上将近两周
问题是我在加载评论时解开可选值时意外收到错误致命错误..
我正在尝试为评论加载JSON数据 在PopularShotsCollectionViewController
import UIKit
import FMMosaicLayout
private let reuseIdentifier = "Cell"
class PopularShotsCollectionViewController: UICollectionViewController {
private var shots : [Shot] = [Shot](){
didSet{
self.collectionView?.reloadData()
}
}
var API_URL = Config.SHOT_URL
var shotPages = 1
var shot : Shot!
var comments : [Comment] = [Comment]()
var tableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let layout : FMMosaicLayout = FMMosaicLayout()
self.collectionView?.collectionViewLayout = layout
// Do any additional setup after loading the view.
self.title = "Popular"
self.API_URL = Config.POPULAR_URL
self.loadShots()
self.loadComments()
}
func loadComments(){
// When i debug shot.commentsUrl does not return nil
DribbleObjectHandler.getComments(shot.commentsUrl) { (comments) -> Void in
self.comments = comments
}
}
getCommentsMethod的代码
class func getComments(commentsUrl : String, completion:(([Comment]) -> Void)) {
var comments = [Comment]()
let url = commentsUrl + "&access_token=" + Config.ACCESS_TOKEN
HttpService.getJSON(url) { (jsonData) -> Void in
for commentData in jsonData {
let comment = Comment(data: commentData as! NSDictionary)
comments.append(comment)
}
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), { () -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
completion(comments)
})
})
}
}
HttpService的代码
import Foundation
import UIKit
import Alamofire
class HttpService {
class func getJSON(url: String, callback:((NSArray) -> Void)) {
let nsURL = NSURL(string: url)!
Alamofire.request(.GET, nsURL).response { (request, response, data, error) -> Void in
if error != nil{
print("error")
}
if data != nil {
let jsonData = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSArray
print(jsonData)
callback(jsonData)
}
}
}
}
如果您需要更多代码来解决问题我非常高兴
而在PopularShotsCollectionViewController我也一直在加载镜头 使用相同的方法
func loadShots(){
DribbleObjectHandler.getShots(API_URL) { (shots) -> Void in
self.shots = shots
}
}
但我没有错误,它完美无缺 get Shots的代码是一样的。我们只需访问loadShots中的Shot类,然后访问loadComments中的Comment Class
评论类
import Foundation
import UIKit
class Comment {
var id : Int!
var body : String!
var date : String!
var user : User!
init(data : NSDictionary){
self.id = data["id"] as! Int
let bodyHTML = Utils.getStringFromJSON(data, key: "body")
self.body = Utils.stripHTML(bodyHTML)
let dateInfo = Utils.getStringFromJSON(data, key: "created_at")
self.date = Utils.formatDate(dateInfo)
if let userData = data["user"] as? NSDictionary {
self.user = User(data: userData)
}
}
}
镜头类代码
import Foundation
class Shot: DribbbleBase {
var imageUrl : String!
var htmlUrl : String!
var commentsUrl : String!
var bucketsUrl : String!
var likesUrl : String!
var attachmentUrl : String!
var reboundUrl : String!
var title : String!
var date : String!
var description : String!
var commentCount : Int!
var viewsCount : Int!
var likesCount : Int!
var bucketsCount : Int!
var attachmentsCount : Int!
var reboundCount : Int!
var user : User!
override init(data: NSDictionary) {
super.init(data: data)
self.commentCount = data["comments_count"] as! Int
self.likesCount = data["likes_count"] as! Int
self.viewsCount = data["views_count"] as! Int
self.bucketsCount = data["buckets_count"] as! Int
self.attachmentsCount = data["attachments_count"] as! Int
self.reboundCount = data["rebounds_count"] as! Int
self.commentsUrl = Utils.getStringFromJSON(data, key: "comments_url")
self.bucketsUrl = Utils.getStringFromJSON(data, key: "buckets_url")
self.likesUrl = Utils.getStringFromJSON(data, key: "likes_url")
self.title = Utils.getStringFromJSON(data, key: "title")
self.attachmentUrl = Utils.getStringFromJSON(data, key: "attachments_url")
self.reboundUrl = Utils.getStringFromJSON(data, key: "rebounds_url")
let dateInfo = Utils.getStringFromJSON(data, key: "created_at")
self.date = Utils.formatDate(dateInfo)
let desc = Utils.getStringFromJSON(data, key: "description")
self.description = Utils.stripHTML(desc)
let images = data["images"] as! NSDictionary
self.imageUrl = Utils.getStringFromJSON(images, key: "normal")
//let tags = data["tags"] as! NSArray
if let userData = data["user"] as? NSDictionary {
self.user = User(data: userData)
}
}
}
和DribbbleBase代码
import Foundation
class DribbbleBase {
var id: Int
init(data: NSDictionary){
self.id = data["id"] as! Int
}
}
请帮助我..请记住,我对Swift来说还是个新手
先谢谢 雅利安
答案 0 :(得分:0)
你在PopularShotsCollectionViewController中声明了拍摄属性而你没有初始化它所以拍摄是零。然后你试图在loadComments方法中访问shot的commentsUrl属性,该属性为nil。您应该在视图控制器的init或view did load方法中实例化镜头