UiCollectionView Cell Selection Error - ios

I have used Alamofire and SwiftyJSON To Populate the UiCollectionview and its working fine but didSelectItemAtIndexPath function shows array out of index thought I have printed the array count and it's not empty
any suggestion
here is my code:-
The model
import Foundation
class ProductModel {
private var _ProductItemId: String!
private var _ProductMainCategory: String!
private var _ProductCategoryId: String!
private var _ProductName: String!
private var _ProductItemNo: String!
private var _ProductAvalaibility: String!
private var _ProductSeoDesc: String!
private var _ProductImageURL: String!
private var _ProductBrand_ID: String!
private var _ProductCat_name: String!
//Level 1
private var _ProductTotalQuantity : String!
private var _Productprice : String!
private var _ProductSalePrice : String!
private var _ProductWeightName : String!
private var _ProductCode : String!
var ProductItemId : String {
return _ProductItemId
}
var ProductMainCategory : String {
return _ProductMainCategory
}
var ProductCategoryId : String {
return _ProductCategoryId
}
var ProductName : String {
return _ProductName
}
var ProductItemNo : String {
return _ProductItemNo
}
var ProductAvalaibility : String {
return _ProductAvalaibility
}
var ProductSeoDesc : String {
return _ProductSeoDesc
}
var ProductImageURL : String {
return _ProductImageURL
}
var ProductBrand_ID: String {
return _ProductBrand_ID
}
var ProductCat_name: String {
return _ProductCat_name
}
//Level 1
var ProductTotalQuantity : String {
return _ProductTotalQuantity
}
var Productprice : String {
return _Productprice
}
var ProductSalePrice : String {
return _ProductSalePrice
}
var ProductWeightName : String {
return _ProductWeightName
}
var ProductCode : String {
return _ProductCode
}
//Initilizer
init(ProductImageURL : String, ProductName : String, Productprice : String, ProductSalePrice : String)
{
self._ProductName = ProductName
self._ProductImageURL = ProductImageURL//
//Level 1
self._Productprice = Productprice//
self._ProductSalePrice = ProductSalePrice//
}
My CollectionView Delegates and Data sources
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ProductCell", forIndexPath: indexPath)as? ProductCell {
let _prod: ProductModel!
_prod = prod [indexPath.row]
cell.configureCell(_prod)
return cell
}
else{
return UICollectionViewCell()
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let prodDetail: ProductModel!
prodDetail = prod[indexPath.row] //error Array index out of range
print(prodDetail.Productprice)
performSegueWithIdentifier("productDetailSegue", sender: prodDetail)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//if inSearchMode{
//return filteredProd.count
// }
return prod.count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
Calling API and Parsing
Alamofire.request(.POST, "http://www.picknget.com/webservice/index.php/Home/filter_grocery_product_practice/", parameters: parameterDictionary as? [String : AnyObject])
.responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
print(json)
if let _statusCode = json["status"].string {
// print("the ststus code is ", _statusCode)
if (_statusCode == "1"){
self.parseJSON(json)
}
if (_statusCode == "0"){
SwiftSpinner.hide({
self.callAlert("OOP's", _msg: "No More Product is available in this section right now")
})
}
}
//print ("json result ", json)
}
}.responseString { response in
//print("response ",response.result.value)
}
}
func parseJSON(json: JSON) {
for result in json["cat"].arrayValue {
let name = result["Name"]
let aString: String = "\(result["ImageURL"])"
let product_Image_Url = aString.stringByReplacingOccurrencesOfString("~", withString: "http://www.picknget.com", options: NSStringCompareOptions.LiteralSearch, range: nil)
let price = result["cat_price"][0]["Price"].string
let SalePrice = result["cat_price"][0]["SalePrice"].string
let product = ProductModel(ProductImageURL: "\(product_Image_Url)", ProductName: "\(name)", Productprice: "\(price!)", ProductSalePrice: "\(SalePrice!)")
prod.append(product)
}
print("########")
print(prod.count)
dispatch_async(dispatch_get_main_queue(),{
self.productCollect.reloadData()
});
}

According your comments, I believe the issue is related to how you set the obtained Products for the Collection View.
It's very likely that the function parseJSON executes on a secondary thread. This is actually, the same execution context of the completion handler of method responseJSON.
Within function parseJSON you have this statement:
prod.append(product)
Here, prod should not be a global variable or not a member variable of the view controller! Make it a local variable in function parseJSON!
Your view controller should have a property of this array as well, e.g. products. This serves as the "model" of the view controller. It will be accesses only from the main thread.
In parseJSON assign the view controller the products as follows:
func parseJSON(json: JSON) {
var tmpProducts: [Product] = []
for result in json["cat"].arrayValue {
let name = result["Name"]
let aString: String = "\(result["ImageURL"])"
let product_Image_Url = aString.stringByReplacingOccurrencesOfString("~", withString: "http://www.picknget.com", options: NSStringCompareOptions.LiteralSearch, range: nil)
let price = result["cat_price"][0]["Price"].string
let SalePrice = result["cat_price"][0]["SalePrice"].string
let product = ProductModel(ProductImageURL: "\(product_Image_Url)", ProductName: "\(name)", Productprice: "\(price!)", ProductSalePrice: "\(SalePrice!)")
tmpProducts.append(product)
}
dispatch_async(dispatch_get_main_queue(),{
self.products = tmpProducts // assuming `self` is the ViewController
self.productCollect.reloadData()
});
}
Note: you need to change your Data Source Delegates accordingly, e.g. accessing the "model" (self.products.count) etc.

Related

Remove duplicate elements from object array

I am attempting to remove duplicate elements of my Transaction object. The Transactions are being loaded from Firestore and displayed onto a UTableView. I tried to follow this answer [here][1] however I got an error that budgetData is not hashable. Is there a way I can remove duplicate Transactions that have the same "transId" and return an updated array of budgetdata?
var budgetData: [Transaction] = []
func loadCatTransactions(){
if let catId = self.categoryId{
guard let user = Auth.auth().currentUser?.uid else { return }
print("userFromLoadChat::\(user)")
db.collection("users").document(user).collection("Transactions")
.whereField("catId", isEqualTo: catId)
.getDocuments() {
snapshot, error in
if let error = error {
print("\(error.localizedDescription)")
} else {
self.budgetData.removeAll()
for document in snapshot!.documents {
let data = document.data()
let title = data["name"] as? String ?? ""
let date = data["date"] as? String ?? ""
let amount = data["amount"] as? Double ?? 0
let id = data["transId"] as? String ?? ""
let trans = Transaction(catId:catId,title: title, dateInfo: date, image: UIImage.groceriesIcon, amount: amount)
self.budgetData.append(trans)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
}
func uniq<S : Sequence, T : Hashable>(source: S) -> [T] where S.Iterator.Element == T {
var buffer = [T]()
var added = Set<T>()
for elem in source {
if !added.contains(elem) {
buffer.append(elem)
added.insert(elem)
}
}
return buffer
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.budgetData.count
}
struct Transaction {
var catId : String? = nil
var title: String
var dateInfo: String
var image: UIImage
var amount: Double
var annualPercentageRate: Double?
var trailingSubText: String?
var uid: String?
var outOfString: String?
var category: String?
var dictionary:[String:Any]{
return [
"title": title,
"dateInfo":dateInfo,
"amount":amount,
"annualPercentageRate":annualPercentageRate,
"trailingSubText":trailingSubText,
"uid": uid,
"outOfString": outOfString,
"category": category
]
}
}
[1]: Removing duplicate elements from an array in Swift
You need to make Transaction object Hashable. Try this
struct Transaction{
var transId: String
}
extension Transaction: Hashable{
static func ==(lhs: Transaction, rhs: Transaction) -> Bool {
return lhs.transId == rhs.transId
}
}
var budgetData = [Transaction(transId: "a"), Transaction(transId:"c"),
Transaction(transId: "a"), Transaction(transId: "d")]
var tranSet = Set<Transaction>()
budgetData = budgetData.filter { (transaction) -> Bool in
if !tranSet.contains(transaction){
tranSet.insert(transaction)
return true
}
return false
}

How to call the correct constructor when using generic T.Type class on Swift4?

I have a database with some tables, each table represents a object of my project. I want write a generic function to read, by SQL, a table and create a object with the records readed. So, the parameters of my function are: Table Name and Object Type. The code below is my func to do this. In the end of func, I tries call what I would like to do, but with a especific object, that's don't the I want.
func readAll<T>(objeto: String, typeClass: T.Type) -> [T] {
var ret : [T] = []
// STATEMENT DATA
let queryString = "SELECT * FROM \(objeto);"
var queryStatement: OpaquePointer? = nil
// STATEMENT DATA TYPE
let queryString2 = "PRAGMA table_info(\(objeto));"
var queryStatement2: OpaquePointer? = nil
// 1
if sqlite3_prepare_v2(db,queryString,-1,&queryStatement,nil) != SQLITE_OK {
print("Error to compile readAll \(objeto) 1")
return ret
}
if sqlite3_prepare_v2(db,queryString2,-1,&queryStatement2,nil) != SQLITE_OK {
print("Error to compile readAll \(objeto) 2")
return ret
}
var listNameColumns : [String] = []
while( sqlite3_step(queryStatement2) == SQLITE_ROW ) {
listNameColumns.append( String(cString: sqlite3_column_text(queryStatement2, 2)!) )
}
// 2
while ( sqlite3_step(queryStatement) == SQLITE_ROW ) {
var dict: [String:Any] = [:]
for i in 0...listNameColumns.count-1 {
let nameColumn = String(cString: sqlite3_column_name(queryStatement,Int32(i))!)
switch (sqlite3_column_type(queryStatement, Int32(i))) {
case SQLITE_TEXT:
dict[nameColumn] = String(cString: sqlite3_column_text(queryStatement, Int32(i))!)
break
case SQLITE_INTEGER:
dict[nameColumn] = sqlite3_column_int(queryStatement, Int32(i))
break
case SQLITE_FLOAT:
dict[nameColumn] = sqlite3_column_double(queryStatement, Int32(i))
break
default:
print("Tipo desconhecido.")
break
}
}
ret.append(ResPartner(dict: dict)) <------ HERE IS MY QUESTION!
}
// 3
sqlite3_finalize(queryStatement2)
sqlite3_finalize(queryStatement)
return ret
}
Here are two objects, They are a bit different, but the builder works the same and the fields as well.
class ResPartner {
static let fieldsResPartner : [String] = ["id","company_type_enum_for_customer","name","contact_address","customer_account_number","customer_group_id","segment_id","subsegment_id","economic_group_id","street","category_id","type_stablishment_id","final_user","final_taxpayer","cnpj_cpf","inscr_est","ccm","cnae","phone","phone_extension","mobile","fax","email","email_extra","website","lang"]
var attributes : [String:Any] = [:]
init(dict : [String:Any]) {
for k in dict.keys {
if(ResPartner.fieldsResPartner.contains(k)) {
attributes[k] = dict[k]
}
}
}
func toString() {
for k in attributes.keys{
print("\(k) - \(attributes[k]!)")
}
}
}
class Product {
static let fieldsProducts : [String] = ["id","name","default_code","display_name","categ_id","company_ax_id","destination_type","fiscal_class_code","multiple","taxes_id","uom_id","uom_po_id","__last_update","active","create_date","create_uid","currency_id","invoice_police","item_ids","list_price","price","pricelist_id","type"]
public var default_code: String!
public var display_name: String!
public var id: Int!
public var name: String!
public var destination_type: String!
public var company_ax_id: Int!
public var categ_id: Int!
public var fiscal_class_code: String!
public var taxes_id: Int!
public var uom_id: Int!
public var uom_po_id: Int!
public var multiple: Int!
public var last_update: String!
public var active: Bool!
public var create_date: String!
public var create_uid: Int!
public var currency_id: Int!
public var invoice_police: String!
public var item_ids: [Int]!
public var list_price: String!
public var price: Float!
public var pricelist_id: Int!
public var type: String!
init() {
}
init( dict : [String:Any] ) {
self.default_code = dict["default_code"] as! String
self.display_name = dict["display_name"] as! String
self.id = dict["id"] as! Int
self.name = dict["name"] as! String
self.destination_type = dict["destination_type"] as! String
self.company_ax_id = dict["company_ax_id"] as! Int
self.categ_id = dict["categ_id"] as! Int
self.fiscal_class_code = dict["fiscal_class_code"] as! String
self.taxes_id = dict["taxes_id"] as! Int
self.uom_id = dict["uom_id"] as! Int
self.uom_po_id = dict["uom_po_id"] as! Int
self.multiple = dict["multiple"] as! Int
self.last_update = dict["last_update"] as! String
self.active = dict["active"] as! Bool
self.create_date = dict["create_date"] as! String
self.create_uid = dict["create_uid"] as! Int
self.currency_id = dict["currency_id"] as! Int
self.invoice_police = dict["invoice_police"] as! String
self.item_ids = dict["item_ids"] as! [Int]
self.list_price = dict["list_price"] as! String
self.price = dict["price"] as! Float
self.pricelist_id = dict["pricelist_id"] as! Int
self.type = dict["type"] as! String
}
}
So, my question is, How I call the constructor of T.Type class passed by parameter? I did read about protocols, extensions, other posts, but not solves my problem.
You can constrain your generic with protocol:
Define a protocol for initializing with a dictionary:
protocol DictionaryInitializable {
init(dict: [String: Any])
}
Make your two types conform to that type (you'll have to add required to your init methods, as prompted by Xcode), e.g.:
class Product: DictionaryInitializable {
...
required init(dict: [String: Any]) {
...
}
}
and
class ResPartner: DictionaryInitializable {
static let fieldsResPartner = ...
var attributes: [String: Any] = [:]
required init(dict: [String: Any]) {
for k in dict.keys where ResPartner.fieldsResPartner.contains(k) {
attributes[k] = dict[k]
}
}
func toString() { ... }
}
Change your method declaration to make it clear that T must conform to your new protocol:
func readAll<T: DictionaryInitializable>(objeto: String, typeClass: T.Type) -> [T] {
var ret: [T] = []
...
// 2
while ( sqlite3_step(queryStatement) == SQLITE_ROW ) {
var dict: [String: Any] = [:]
...
ret.append(T(dict: dict)) // You can now use `T` here
}
return ret
}
And you’d call it like:
let list = db_respartner.readAll(objeto: "res_partner", typeClass: ResPartner.self)
Create a Protocol with init Method
protocol Mappable {
init(dictionary:[String:AnyObject]) // Changed based on your requirement.
}
Conform your protocol in ResPartner.
class ResPartner: Mappable {
required init(dictionary : [String : AnyObject]) {
// initialize here
}
}
Conform your protocol in Product.
class Product: Mappable {
required init(dictionary : [String : AnyObject]) {
// initialize here
}
}
Create a custom objects
func readAll<T:Mappable>(objeto: String, typeClass: T.Type) -> [T] {
var ret : [T] = []
// intialize your variable
let obj = typeClass.init(dictionary: ["":"" as AnyObject])
return ret
}

JSON from Object with array issue in iOS Swift 3

public class LessonAssignment {
private var title : String?
private var category : String?
private var week : Int?
private var day : Int?
//Title
public func getTitle() -> String {
return title!
}
public func setTitle(title : String) {
self.title = title
}
//Category
public func getCategory() -> String {
return category!
}
public func setCategory(category : String) {
self.category = category
}
//Week
public func getWeek() -> Int {
return week!
}
public func setWeek(week : Int) {
self.week = week
}
//Day
public func getDay() -> Int {
return day!
}
public func setDay(day : Int) {
self.day = day
}
/**
Returns an array of models based on given dictionary.
Sample usage:
let lessonAssignment_list = LessonAssignment.modelsFromDictionaryArray(someDictionaryArrayFromJSON)
- parameter array: NSArray from JSON dictionary.
- returns: Array of LessonAssignment Instances.
*/
public class func modelsFromDictionaryArray(array:NSArray) -> [LessonAssignment]
{
var models = [LessonAssignment]()
for item in array {
models.append(LessonAssignment(dictionary: item as! NSDictionary)!)
}
return models
}
/**
Constructs the object based on the given dictionary.
Sample usage:
let lessonAssignment = LessonAssignment(someDictionaryFromJSON)
- parameter dictionary: NSDictionary from JSON.
- returns: LessonAssignment Instance.
*/
init() { }
required public init?(dictionary: NSDictionary) {
title = dictionary["title"] as? String
category = dictionary["category"] as? String
week = dictionary["week"] as? Int
day = dictionary["day"] as? Int
}
/**
Returns the dictionary representation for the current instance.
- returns: NSDictionary.
*/
public func dictionaryRepresentation() -> NSDictionary {
let dictionary = NSMutableDictionary()
dictionary.setValue(self.title, forKey: "title")
dictionary.setValue(self.category, forKey: "category")
dictionary.setValue(self.week, forKey: "week")
dictionary.setValue(self.day, forKey: "day")
return dictionary
}
func toDictionary() -> [String : Any] {
var dictionary = [String:Any]()
let otherSelf = Mirror(reflecting: self)
for child in otherSelf.children {
if let key = child.label {
dictionary[key] = child.value
}
}
return dictionary
}
}
public class LessonPlan {
private var name : String?
private var weeks : Int?
private var days : Int?
private var hours : Int?
private var lessonAssignment = [LessonAssignment]()
private var lessonNote = [LessonNote]()
//Name
public func getName() -> String {
if name == nil {
return ""
} else {
return name!
}
}
public func setName(name : String) {
self.name = name
}
//Weeks
public func getWeeks() -> Int {
if weeks == 0 {
return 0
} else {
return weeks!
}
}
public func setWeeks(weeks : Int) {
self.weeks = weeks
}
//Days
public func getDays() -> Int {
if days == 0 {
return 0
} else {
return days!
}
}
public func setDays(days : Int) {
self.days = days
}
//Hours
public func getHours() -> Int {
if days == 0 {
return 0
} else {
return hours!
}
}
public func setHours(hours : Int) {
self.hours = hours
}
//LessonAssignment
public func getLessonAssignment() -> [LessonAssignment] {
return lessonAssignment
}
public func setLessonAssignment(lessonAssignment : [LessonAssignment]) {
self.lessonAssignment = lessonAssignment
}
//LessonNote
public func getLessonNote() -> [LessonNote] {
return lessonNote
}
public func setLessonNote(lessonNote : [LessonNote]) {
self.lessonNote = lessonNote
}
/**
Returns an array of models based on given dictionary.
Sample usage:
let lessonPlan_list = LessonPlan.modelsFromDictionaryArray(someDictionaryArrayFromJSON)
- parameter array: NSArray from JSON dictionary.
- returns: Array of LessonPlan Instances.
*/
public class func modelsFromDictionaryArray(array:NSArray) -> [LessonPlan]
{
var models:[LessonPlan] = []
for item in array
{
models.append(LessonPlan(dictionary: item as! NSDictionary)!)
}
return models
}
/**
Constructs the object based on the given dictionary.
Sample usage:
let lessonPlan = LessonPlan(someDictionaryFromJSON)
- parameter dictionary: NSDictionary from JSON.
- returns: LessonPlan Instance.
*/
init() { }
required public init?(dictionary: NSDictionary) {
name = dictionary["name"] as? String
weeks = dictionary["weeks"] as? Int
days = dictionary["days"] as? Int
hours = dictionary["hours"] as? Int
lessonAssignment = LessonAssignment.modelsFromDictionaryArray(array:dictionary["lessonAssignment"] as! NSArray)
lessonNote = LessonNote.modelsFromDictionaryArray(array: dictionary["lessonNote"] as! NSArray)
}
/**
Returns the dictionary representation for the current instance.
- returns: NSDictionary.
*/
public func dictionaryRepresentation() -> NSDictionary {
let dictionary = NSMutableDictionary()
dictionary.setValue(self.name, forKey: "name")
dictionary.setValue(self.weeks, forKey: "weeks")
dictionary.setValue(self.days, forKey: "days")
dictionary.setValue(self.hours, forKey: "hours")
return dictionary
}
func toDictionary() -> [String : Any] {
var dictionary = [String:Any]()
let otherSelf = Mirror(reflecting: self)
for child in otherSelf.children {
print("Child = \(child)")
print("Child Label = \(String(describing: child.label))")
print("Child Value = \(child.value)")
if let key = child.label {
dictionary[key] = child.value
}
}
return dictionary
}
}
public class ServerRequest {
private var lessonPlan : LessonPlan?
init() {
}
//LessonPlan
public func getLessonPlan() -> LessonPlan {
return lessonPlan!
}
public func setLessonPlan(lessonPlan : LessonPlan) {
self.lessonPlan = lessonPlan
}
func toDictionary() -> [String : Any] {
var dictionary = [String:Any]()
let otherSelf = Mirror(reflecting: self)
for child in otherSelf.children {
if let key = child.label {
dictionary[key] = lessonPlan?.toDictionary()
}
}
return dictionary
}
}
Current Response = ["lessonPlan": ["name": "test", "days": 2, "weeks": 1,
"hours": 1, "lessonAssignment": [HSP.LessonAssignment,
HSP.LessonAssignment], "lessonNote": []]]
Expected Response = ["lessonPlan": ["name": "test", "days": 2,
"weeks": 1, "hours": 1, "lessonAssignment": [["title" : "some value 1", "category" : "some value", "week" : 1, "day" : 2]], "lessonNote": []]]
Instead of the LessinAssignment Object I would like to add the actual values which is array. Any clue how to resolve this. I know i have to add some more logic inside the toDictionary method and based on the key "lessonAssignment" I have to get each array and add the value in the key as final Array.
From my comment, something like this:
assignments.map({
(value: HSP.LessonAssignment) -> NSDictionary in
return value.toDictionary()
})

Swift - Grab data from Firebase and store it into an array

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
}
}

Swift dictionary deletes previous data when appending new data

I have a swift dictionary with custom objects and I use it to fill my UITableview. However, when I fill the dictionary, I realize that it deletes previous data when I append a new data to it. The related code blocks like below:
var dict = Dictionary<ListItem, [ListItem]>()
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.dict.keys.array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: CategoryCell! = tableView.dequeueReusableCellWithIdentifier("categoryCell", forIndexPath: indexPath) as? CategoryCell
if(cell == nil)
{
cell = CategoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: "categoryCell")
}
else
{
cell.ClearCurrentCellContext(cell.image, labelTitle: cell.lblCategory, labelDescription: cell.lblDescription)
}
if let ip = indexPath as NSIndexPath?
{
let key = Array(self.dict.keys)[ip.row] as ListItem
cell.SetCell(key, image: cell.image, labelTitle: cell.lblCategory, labelDescription: cell.lblDescription)
}
return cell
}
And I fill the dictionary inside of this block:
Alamofire.request(.POST, WSConstants.WebServiceAddress, parameters: nil)
.responseJSON
{(request, response, JSON, error) in
if let jsonResult: NSDictionary = JSON as? NSDictionary
{
if let categories = jsonResult[WSConstants.CATEGORIES] as? NSDictionary
{
if let wsStatus = categories.valueForKey(WSConstants.WS_STATUS) as? String
{
if(wsStatus == "ok")
{
var mainObj: NSDictionary!
var mainItem: ListItem!
for(var i=0; i<mainJSONArray.count; i++)
{
mainObj = mainJSONArray.objectAtIndex(i) as! NSDictionary
mainItem = ListItem()
mainItem.itemId = mainObj.valueForKey(WSConstants.ID) as! Int
mainItem.itemName = mainObj.valueForKey(WSConstants.TITLE) as! String
mainItem.itemDescription = mainObj.valueForKey(WSConstants.DESCRIPTION) as! String
mainItem.itemIcon = mainObj.valueForKey(WSConstants.ICON) as! String
mainItem.parentId = mainObj.valueForKey(WSConstants.PARENT) as! Int
mainItem.postCount = mainObj.valueForKey(WSConstants.POST_COUNT) as! Int
if let subJSONArray = mainObj.valueForKey(WSConstants.SUB) as? NSArray
{
var midCategoryList: [ListItem]! = [ListItem]()
var subObj: NSDictionary!
var subItem: ListItem!
for(var i=0; i<subJSONArray.count; i++)
{
subObj = subJSONArray.objectAtIndex(i) as! NSDictionary
subItem = ListItem()
subItem.itemId = subObj.valueForKey(WSConstants.ID) as! Int
subItem.itemName = subObj.valueForKey(WSConstants.TITLE) as! String
subItem.itemDescription = subObj.valueForKey(WSConstants.DESCRIPTION) as! String
subItem.itemIcon = subObj.valueForKey(WSConstants.ICON) as! String
subItem.parentId = subObj.valueForKey(WSConstants.PARENT) as! Int
subItem.postCount = subObj.valueForKey(WSConstants.POST_COUNT) as! Int
midCategoryList.append(subItem)
subItem = nil
subObj = nil
}
// The code below line fills the dictionary with key and its values
self.dict[mainItem] = midCategoryList
midCategoryList = nil
}
mainItem = nil
mainObj = nil
}
Utility.ReloadTableViewDataWithAnimations(self.categoryTableView)
}
}
}
}
ListItem class:
class ListItem: Serializable, Hashable, NSCoding
{
var uniqueID: Int = 0
override var hashValue: Int { return uniqueID.hashValue }
var itemId: Int
var itemName: String!
var itemIcon: String!
var itemDescription: String!
var parentId: Int!
var postCount: Int!
var content: String!
override init()
{
self.itemId = 0
self.itemName = ""
self.itemIcon = ""
self.parentId = 0
self.postCount = 0
self.content = ""
self.itemDescription = ""
}
init(itemId: Int, itemName: String!, itemIcon: String!, itemDescription: String!, parentId: Int!, postCount: Int, content: String!, date: String!, url: String!)
{
self.itemId = itemId
self.itemName = itemName
self.itemIcon = itemIcon
self.parentId = parentId
self.postCount = postCount
self.content = content
self.itemDescription = itemDescription
}
deinit
{
}
required init(coder aDecoder: NSCoder)
{
self.itemId = aDecoder.decodeIntegerForKey("itemId") as Int
self.itemName = aDecoder.decodeObjectForKey("itemName") as! String
self.itemIcon = aDecoder.decodeObjectForKey("itemIcon") as! String
self.itemDescription = aDecoder.decodeObjectForKey("itemDescription") as! String
}
func encodeWithCoder(aCoder: NSCoder)
{
aCoder.encodeInteger(self.itemId, forKey: "itemId")
aCoder.encodeObject(self.itemName, forKey: "itemName")
aCoder.encodeObject(self.itemIcon, forKey: "itemIcon")
aCoder.encodeObject(self.itemDescription, forKey: "itemDescription")
}
}
func ==(lhs: ListItem, rhs: ListItem) -> Bool
{
return lhs.uniqueID == rhs.uniqueID
}
Is this a swift bug or I make some mistakes to fill it ?
Thank you for your answers
King regards
Solution:
The reason of the problem is that using == instead of === in ListItem class. Daniel T.'s solution is the right answer. Thank you all.
Without seeing more code, my guess is that the problem lies in your ListItem's == operator. The following code does not exhibit the problem you are describing:
//: Playground - noun: a place where people can play
class ListItem: Hashable {
var itemId: Int = 0
var itemName: String = ""
var itemDescription: String = ""
var itemIcon: String = ""
var parentId: Int = 0
var postCount: Int = 0
var hashValue: Int {
return itemId.hashValue
}
}
func ==(lhs: ListItem, rhs: ListItem) -> Bool {
return lhs === rhs // simple identity
}
var dict = Dictionary<ListItem, [ListItem]>()
let arr = [ListItem(), ListItem()]
dict[ListItem()] = arr
dict[ListItem()] = arr
dict.keys.array.count

Resources