I have problem with use data from firebase after get them. I written function getData() in model, use delegate to call them on UITableViewController and set data to TableView.
But when I create new array to get data from func getData(), this array is nil.
This is my model:
import Foundation
import Firebase
protocol myDelegate: class {
func didFetchData(datas: [Book])
}
class Book {
var Id: String?
var Author: String?
var ChapterCount: Int?
var CoverPhoto: String?
var Genre: String?
var Image: String?
var Intro: String?
var Like: Int?
var Name: String?
var Status: String?
var UpdateDay: String?
var UploadDay: String?
var View: Int?
var ref: DatabaseReference!
weak var delegate: myDelegate?
init()
{
}
init(Id: String,Author: String,Image: String,Name: String,Status: String,UpdateDay: String,View: Int)
{
self.Id = Id
self.Author = Author
self.Image = Image
self.Name = Name
self.Status = Status
self.UpdateDay = UpdateDay
self.View = View
}
func getListBook() {
ref = Database.database().reference()
ref.child("Book").observe(.value, with: { snapshot in
var newNames: [Book] = []
let value = snapshot.value as? NSDictionary
for nBook in value! {
let val = nBook.value as? NSDictionary
self.Name = val?["Name"] as? String ?? ""
self.Author = val?["Author"] as? String ?? ""
self.View = val?["View"] as? Int ?? 0
self.Status = val?["Status"] as? String ?? ""
self.Id = val?["Id"] as? String ?? ""
self.Image = val?["Image"] as? String ?? ""
self.UpdateDay = val?["UpdateDay"] as? String ?? ""
newNames.append(Book(Id: self.Id!, Author: self.Author!, Image: self.Image!, Name: self.Name!, Status: self.Status!, UpdateDay: self.UpdateDay!, View: self.View!))
}
self.delegate?.didFetchData(datas: newNames)
})
}
}
And there is class UITableViewController:
import Firebase
class ListStoryTableView: UITableViewController, myDelegate {
var ref: DatabaseReference!
var book = Book()
var listBook: [Book] = []
func didFetchData(datas: [Book]) {
listBook = datas
}
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib.init(nibName: "ListStoryTableViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "ListStoryTableViewCell")
book.delegate = self
book.getListBook()
print("\(listBook)") //this is return 0
}```
One solution would be to change-remove your protocol implementation and use a completion block in your getListBook func. Delete myDelegate reference from your ListStoryTableView and do the following change:
func getListBook(completion: #escaping (_ books: [Book]) -> Void) {
ref = Database.database().reference()
ref.child("Book").observe(.value, with: { snapshot in
var newNames: [Book] = []
let value = snapshot.value as? NSDictionary
for nBook in value! {
let val = nBook.value as? NSDictionary
self.Name = val?["Name"] as? String ?? ""
self.Author = val?["Author"] as? String ?? ""
self.View = val?["View"] as? Int ?? 0
self.Status = val?["Status"] as? String ?? ""
self.Id = val?["Id"] as? String ?? ""
self.Image = val?["Image"] as? String ?? ""
self.UpdateDay = val?["UpdateDay"] as? String ?? ""
newNames.append(Book(Id: self.Id!, Author: self.Author!, Image: self.Image!, Name: self.Name!, Status: self.Status!, UpdateDay: self.UpdateDay!, View: self.View!))
}
completion(newNames)
})
}
and then in your viewDidLoad or any other function you use the following to fetch your data:
book.getListBook { books in
listBook = books
tableView.reloadData()
}
I hope that helps you.
func didFetchData(datas: [Book]) {
listBook = datas
print("\(listBook)")
}
print listBook in this function and you will have the data..
Related
I try to send the voting from users to firebase and save them under the specific user.
class User: NSObject {
var id: String?
init(dictionary: [String: AnyObject]) {
self.id = dictionary["id"] as? String
}
var ref: DatabaseReference!
var numberOfGood = 0
init(id: String? = nil) {
self.id = id
ref = Database.database().reference().child("users").childByAutoId()
}
init(snapshot: DataSnapshot){
ref = snapshot.ref
if let value = snapshot.value as? [String : Any] {
numberOfGood = value["numberOfGood"] as! Int
}
}
func save() {
let postDictionary = [
"id" : self.id,
"numberOfGood" : self.numberOfGood,
] as [String : Any]
self.ref.setValue(postDictionary)
}
}
Inside the viewController where to vote I handle the voting itself like this:
class UserRatingClass {
var numberOfGood = 0
var ref = Database.database().reference().child("users").childByAutoId()
func good() {
numberOfGood += 1
ref.child("numberOfGood").setValue(numberOfGood)
}
}
var userRating: UserRatingClass! {
didSet {
let x = userRating.numberOfGood
self.good.setTitle("\(x) 👍", for: [])
}
}
#IBAction func goodReview(_ sender: UIButton) {
userRating.good()
let x = userRating.numberOfGood
self.good.setTitle("\(x) 👍", for: [])
}
I tried different ways like
var StringGood = String(user?.numberOfGood)
self.ref.child("users").child(StringGood).setValue(x)
inside the buttonActionFunction but by this I'm always getting Cannot invoke initializer for type 'String' with an argument list of type '(Int?)' as an error...
Edit: I call the User.swift class like this:
var user: User?
So I have a section in my app which displays events that your followers are going to. I successfully pull the followers and there corresponding events. I then add those events to a bigger array of event objects. I am currently having trouble removing the duplicates I have tried many extensions but it doesn't seem to be working. I figure it is because the array contains different objects with different memory addresses so when I compare them they aren't the same regardless of the fact that the content is. I have been messing with this for a while now and can't really figure anything out. if anyone could help me I would appreciate it.
This is the method grabs the events and returns them to an array in my main VC.
static func showFollowingEvent(for followerKey: String,completion: #escaping (Event) -> Void) {
//getting firebase root directory
let ref = Database.database().reference()
ref.child("users").child(followerKey).child("Attending").observeSingleEvent(of: .value, with: { (attendingSnapshot) in
print(attendingSnapshot)
guard var eventKeys = attendingSnapshot.children.allObjects as? [DataSnapshot] else{return}
for event in eventKeys{
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
EventService.show(forEventKey: event.key, completion: { (event) in
dispatchGroup.leave()
completion(event!)
})
}
}) { (err) in
print("couldn't grab event info",err)
}
} print("couldn't grab event info",err)
}
}
This is the function that receives the events and reloads the collectionView upon getting them.
#objc func grabFriendsEvents(){
print("Attempting to see where your friends are going")
UserService.following { (user) in
for following in user {
print(following.username as Any)
PostService.showFollowingEvent(for: following.uid, completion: { (event) in
self.friendsEvents.append(event)
// self.friendsEvents.append(contentsOf: event)
// leave here
self.allEvents2["Friends Events"] = self.friendsEvents.removeDuplicates()
self.collectionView?.reloadData()
})
}
}
}
I tried these extensions which I saw in previous questions and nothing worked.
extension Array where Element: Equatable {
func removeDuplicates() -> [Element] {
var result = [Element]()
for value in self {
if result.contains(value) == false {
result.append(value)
}
}
return result
}
mutating func removeDuplicatesTwo() {
var result = [Element]()
for value in self {
if !result.contains(value) {
result.append(value)
}
}
self = result
}
}
import Foundation
import FirebaseDatabase.FIRDataSnapshot
#objc(Event)
class Event:NSObject{
var key: String?
let currentEventName: String
let currentEventImage: String
let currentEventPromo: String?
let currentEventDescription: String
//nested properties
let currentEventStreetAddress: String
let currentEventCity: String
let currentEventState: String
let currentEventDate: String?
let currentEventEndDate: String?
let currentEventTime: String?
let currentEventEndTime: String?
let currentEventZip: Int
var category: String
//nested properties stop
var currentAttendCount: Int
var isAttending = false
var eventDictionary: [String: Any]{
let dateDict = ["start:date":currentEventDate, "start:time": currentEventTime,"end:time":currentEventEndTime, "end:date": currentEventEndDate]
return ["event:name":currentEventName,"event:imageURL" : currentEventImage,
"event:description": currentEventDescription, "attend:count": currentAttendCount,
"event:street:address": currentEventStreetAddress,"event:zip": currentEventZip,
"event:state": currentEventState, "event:city": currentEventCity, "event:promo": currentEventPromo ?? "", "event:date": dateDict, "event:category":category]
}
init(currentEventKey: String, dictionary: [String:Any]) {
self.key = currentEventKey
self.currentEventName = dictionary["event:name"] as? String ?? ""
self.currentEventImage = dictionary["event:imageURL"] as? String ?? ""
self.currentEventDescription = dictionary["event:description"] as? String ?? ""
self.currentEventPromo = dictionary["event:promo"] as? String ?? ""
self.currentAttendCount = dictionary["attend:count"] as? Int ?? 0
self.category = dictionary["event:category"] as? String ?? ""
//nested properties
self.currentEventStreetAddress = dictionary["event:street:address"] as? String ?? ""
self.currentEventCity = dictionary["event:city"] as? String ?? ""
self.currentEventState = dictionary["event:state"] as? String ?? ""
self.currentEventZip = dictionary["event:zip"] as? Int ?? 0
let eventDate = dictionary["event:date"] as? [String: Any]
self.currentEventDate = eventDate?["start:date"] as? String ?? ""
self.currentEventTime = eventDate?["start:time"] as? String ?? ""
self.currentEventEndTime = eventDate?["end:time"] as? String ?? ""
self.currentEventEndDate = eventDate?["end:date"] as? String ?? ""
}
init?(snapshot: DataSnapshot) {
guard let dict = snapshot.value as? [String : Any],
let currentEventName = dict["event:name"] as? String,
let currentEventImage = dict["event:imageURL"] as? String,
let currentEventDescription = dict["event:description"] as? String,
let currentEventPromo = dict["event:promo"] as? String,
let category = dict["event:category"] as? String,
let currentEventStreetAddress = dict["event:street:address"] as? String,
let currentEventCity = dict["event:city"] as? String,
let currentEventState = dict["event:state"] as? String,
let currentEventZip = dict["event:zip"] as? Int,
let currentAttendCount = dict["attend:count"] as? Int,
let eventDate = dict["event:date"] as? [String: Any],
let currentEventDate = eventDate["start:date"] as? String,
let currentEventEndDate = eventDate["end:date"] as? String,
let currentEventTime = eventDate["start:time"] as? String,
let currentEventEndTime = eventDate["end:time"] as? String
else { return nil }
self.key = snapshot.key
self.currentEventName = currentEventName
self.currentEventImage = currentEventImage
self.currentEventDescription = currentEventDescription
self.currentEventStreetAddress = currentEventStreetAddress
self.currentEventCity = currentEventCity
self.currentEventState = currentEventState
self.currentEventZip = currentEventZip
self.currentAttendCount = currentAttendCount
self.currentEventPromo = currentEventPromo
self.currentEventDate = currentEventDate
self.currentEventTime = currentEventTime
self.currentEventEndTime = currentEventEndTime
self.category = category
self.currentEventEndDate = currentEventEndDate
}
static func ==(lhs: Event, rhs: Event) -> Bool {
return lhs.key == rhs.key
}
}
Classes of type NSObject will automatically call isEqual() for the contains() method. You can override the superclass's implementation to fit your logic.
If your class HAS to inherit NSObject, then use:
class Event: NSObject {
var key: String?
init(key: String) {
self.key = key
}
override func isEqual(_ object: Any?) -> Bool {
guard let event = object as? Event else { return false }
return self.key == event.key
}
}
var event = Event(key: "abc")
var eventCopy = Event(key: "abc")
extension Array where Element:Equatable {
func removeDuplicates() -> [Element] {
return reduce(into: []) { result, element in
if !result.contains(element) {
result.append(element)
}
}
}
}
var events = [event, eventCopy]
events = events.removeDuplicates()
print(events.count)
If your class does not inherit NSObject, make it conform to the Equatable protocol.
class Event: Equatable {
var key: String?
init(key: String) {
self.key = key
}
static func ==(lhs: Event, rhs: Event) -> Bool {
return lhs.key == rhs.key
}
}
var event = Event(key: "abc")
var eventCopy = Event(key: "abc")
extension Array where Element:Equatable {
func removeDuplicates() -> [Element] {
var result = [Element]()
for value in self {
if result.contains(value) == false {
result.append(value)
}
}
return result
}
}
var events = [event, eventCopy]
events = events.removeDuplicates()
print(events.count)
You have to make Event class conform to Equatable protocol
This is my property list: directory.plist
I would like to know how call the subitems into a ViewController. This is my actual ViewController:
import UIKit
class Page1: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var positionLabel: UILabel!
#IBOutlet weak var phoneLabel: UILabel!
#IBOutlet weak var emailLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Shared.instance.employees.forEach({
nameLabel.text = (("name:", $0.name) as? String)
print("name:", $0.name)
print("position:", $0.position)
print("phone:", $0.phone)
print("email:", $0.email)
print()
})
}
}
And this is the Struct I am using:
import UIKit
struct Employee {
let position: String
let name: String
let email: String
let phone: String
init(dictionary: [String: String]) {
self.position = dictionary["Position"] ?? ""
self.name = dictionary["Name"] ?? ""
self.email = dictionary["Email"] ?? ""
self.phone = dictionary["Phone"] ?? ""
}
}
struct Shared {
static var instance = Shared()
var employees: [Employee] = []
}
Inside the AppDelegate I put this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let url = Bundle.main.url(forResource: "directory", withExtension: "plist"), let array = NSArray(contentsOf: url) as? [[String: String]] {
Shared.instance.employees = array.map{Employee(dictionary: $0)}
}
return true
I do I have to do to call the subitems on my directory.plist? I mean the items inside the key Details. Then I wanna show the Func1, Func2 and Func3.
obs.: (this func is an abbreviation of functionary)
Thanks!
After some changes, now I got the Subitems nil: debugger
Your code in the application delegate is fine. You just need to update your Employee struct with another property to store the Details. But this also means that your employee dictionary isn't just a dictionary of string values. So you need to update the code to handle the proper value types:
struct Employee {
let position: String
let name: String
let email: String
let phone: String
let details: [String:String]
init(dictionary: [String: Any]) {
self.position = (dictionary["Position"] as? String) ?? ""
self.name = (dictionary["Name"] as? String) ?? ""
self.email = (dictionary["Email"] as? String) ?? ""
self.phone = (dictionary["Phone"] as? String) ?? ""
self.details = (dictionary["Details"] as? [String:String]) ?? [:]
}
}
I am trying to grab a list of bars from a Firebase Database and store it in an array so I can display it in a table view.
I have configured Firebase and managed to get data in the app as String, AnyObject dictionary.
Here is my code :
struct Bar {
var latitude: Double?
var longitude: Double?
var name: String!
var phoneNumber: String?
var happyHour: String?
var url: NSURL?
var barLogo: UIImage?
var followers: Int?
var addedToFavorites: Int?
var zipCode: Double?
var area: String?
}
class ViewController: UIViewController {
var ref: FIRDatabaseReference!
var refHandle: UInt!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
refHandle = ref.observe(FIRDataEventType.value , with: {(snapshot) in
let dataDict = snapshot.value as! [String : AnyObject]
}
)
}
Here is my JSON exported from Firebase:
{
"data" : {
"bars" : {
"bar1" : {
"addedToFavorites" : 0,
"area" : "upper east",
"follwers" : 0,
"happyHour" : "m-f 16-19",
"lattitude" : 4412334,
"longitude" : 223455,
"name" : "bar1",
"phone" : 212222,
"url" : "http://www.bar1.com",
"zipCode" : 12345
},
"bar2" : {
"addedToFavorites" : 0,
"area" : "upper west",
"follwers" : 0,
"happyHour" : "f - s 20-22",
"lattitude" : 4443221,
"longitude" : 221234,
"name" : "bar 2",
"phone" : 215555,
"url" : "http://www.bar2.com",
"zipCode" : 54321
}
}
}
}
What would be the best approach for this?
I would like to scale it and download hundreds of bars, so manually grabbing the data from the dictionary and storing it into a Bar struct variable and then appending it to an array is not a path I want to go on.
I need a solution to grab all the bars and somehow adding them to an array (or any other method to display them into a tableView).
Thanks in advance.
I found a way to solve this issue :
First of all I got rid of the struct and created a class :
My class file :
import Foundation
import UIKit
class Bar {
private var _name: String!
private var _area: String!
private var _latitude: Double!
private var _longitude: Double!
private var _followers: Int!
private var _happyHour: String!
private var _phone: Double!
private var _url: String!
private var _zipCode: Double!
private var _addedToFav: Int!
var name: String! {
return _name
}
var area: String! {
return _area
}
var latitude: Double! {
return _latitude
}
var longitude: Double! {
return _longitude
}
var followers: Int! {
return _followers
}
var happyHour: String! {
return _happyHour
}
var phone: Double! {
return _phone
}
var url: String! {
return _url
}
var zipCode: Double! {
return _zipCode
}
var addedToFav: Int! {
return _addedToFav
}
init(name: String,area: String! , latitude: Double, longitude: Double, followers: Int, happyHour: String, phone: Double, url: String, zipCode: Double, addedToFav: Int) {
self._name = name
self._area = area
self._latitude = latitude
self._longitude = longitude
self._followers = followers
self._happyHour = happyHour
self._phone = phone
self._url = url
self._zipCode = zipCode
self._addedToFav = addedToFav
}
init(barData: Dictionary<String, AnyObject>) {
if let name = barData["name"] as? String {
self._name = name
}
if let area = barData["area"] as? String {
self._area = area
}
if let latitude = barData["lattitude"] as? Double {
self._latitude = latitude
}
if let longitude = barData["longitude"] as? Double {
self._longitude = longitude
}
if let followers = barData["followers"] as? Int {
self._followers = followers
}
if let happyHour = barData["happyHour"] as? String {
self._happyHour = happyHour
}
if let phone = barData["phone"] as? Double {
self._phone = phone
}
if let url = barData["url"] as? String {
self._url = url
}
if let zipCode = barData["zipCode"] as? Double {
self._zipCode = zipCode
}
if let addedToFav = barData["addedToFavorites"] as? Int {
self._addedToFav = addedToFav
}
}
}
I created a DataService class with a singleton
Data service class file:
import Foundation
import Firebase
let URL_BASE = FIRDatabase.database().reference()
class DataService {
static let ds = DataService()
private var _REF_BASE = URL_BASE
private var _REF_BARS = URL_BASE.child("data").child("bars")
var REF_BASE: FIRDatabaseReference {
return REF_BASE
}
var REF_BARS: FIRDatabaseReference {
return _REF_BARS
}
}
And my modified viewController file (i did not use a tableViewController)
class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {
#IBOutlet weak var myTableView: UITableView!
var baruri = [Bar]()
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
DataService.ds.REF_BARS.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print(snap)
if let barData = snap.value as? Dictionary<String, AnyObject> {
let bar = Bar(barData: barData)
self.baruri.append(bar)
print(self.baruri)
self.myTableView.reloadData()
}
self.myTableView.reloadData()
}
self.myTableView.reloadData()
}
self.myTableView.reloadData()
}
)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return baruri.count
}
func tableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.myTableView.dequeueReusableCell(withIdentifier: "newCell", for: indexPath) as! NewCell
var baruriTabel: Bar!
baruriTabel = baruri[indexPath.row]
cell.barNameLBl.text = baruriTabel.name
cell.followersNrLbl.text = String(baruriTabel.followers)
cell.areaLbl.text = baruriTabel.area
cell.addedToFavoritesLbl.text = String(baruriTabel.addedToFav)
cell.happyHourLbl.text = baruriTabel.happyHour
cell.urlLbl.text = baruriTabel.url
cell.lattitudeLbl.text = String(baruriTabel.latitude)
cell.longitudeLbl.text = String(baruriTabel.longitude)
cell.phoneLbl.text = String(baruriTabel.phone)
cell.zipCode.text = String(baruriTabel.zipCode)
return cell
}
}
Hi guys i have been stuck in this problem for nearly 2 week
The Problem is that i am getting the error fatal Error Unexpectedly while unwrapping an optional value while i am loading comments..
I am trying to load JSON data For the Comments
in 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
}
}
But i am getting this error:
The code for the 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)
})
})
}
}
Code for the 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)
}
}
}
}
If you need more Code to solve the problem i am more than happy
And in PopularShotsCollectionViewController i have been loading shots also
with the same method
func loadShots(){
DribbleObjectHandler.getShots(API_URL) { (shots) -> Void in
self.shots = shots
}
}
but i get no error and it works perfectly
and code for the get Shots is same.. Just we access The Shot class in loadShots and we access The Comment Class in loadComments
The 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)
}
}
}
Code for the Shot Class
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)
}
}
}
and Code for DribbbleBase
import Foundation
class DribbbleBase {
var id: Int
init(data: NSDictionary){
self.id = data["id"] as! Int
}
}
Please help me.. And Please Bear in mind i am fairly new to Swift
Thanks in Advance
Aryan
you declared shot property in your PopularShotsCollectionViewController and you didn't initialize it so shot is be nil. Then you tried to access commentsUrl property of shot which is nil in the loadComments method. You should instantiate shot in the init or view did load method of your view controller