How to handle array in model class? - ios

How can make model class for this json data
{
total: 41,
totalPages: 4,
valueData: [
{
id: "23",
lastLogin: "0-Jul-2011 11:27:36 AM",
name: "varii"
},
{
id: "24",
lastLogin: "0-Jul-2015 11:27:36 AM",
name: "sarii"
},
{
id: "25",
lastLogin: "0-Jul-2018 11:27:36 AM",
name: "narii"
} ]
}
class OnResponse {
var total: Int?
var totalPages: Int?
init(response: [String: Any]) {
self.total = response["total"]
self.totalPages = response["totalPages"]
}
}
It's not working how can make it ready for work
and how to pass values to controller to model and how to get value from model

Follow the below class structure
class Response {
var total: Int?
var totalPages: Int?
var valueData: [LoginData]?
init(response: [String: Any]) {
self.total = response["total"]
self.totalPages = response["totalPages"]
var items:[LoginData] = ()
for (data in response["valueData"]) {
let login = LoginData(name: data["name"], lastLogin: data["lastLogin"])
items.append(login)
}
self.valueData = items
}
}
class LoginData {
var name: String?
var lastLogin: String?
init(name: String, lastLogin: String) {
self.name = name
self.lastLogin = lastLogin
}
}

you should use "reverseMatches" to retrieve the array, not the "data". May be you can use a third library to convert your json data to a model, such as Unbox, https://github.com/JohnSundell/Unbox.

Model for Your response :
struct ModelName {
var total: Int?
var totalPage: Int?
var reverseMatches: [LoginDetails]?
}
struct LoginDetails {
var id: String?
var lastLogin: String?
var name: String?
}
Parse the api response and assign the values on the appropriate fields. I have made, all the variables are optional.
Assign values like below.
var obj = Model()
obj.total = response["total"] as? Int
obj should be var, because you are going to mutate the struct values. because struct is value based, not reference based

class DataModel{
var total : Int?
var totalPages : Int?
var valueData : [UserModel]?
init(JSON: [String:Any?]){
self = parser.doParse(JSON: JSON)
}
}
class UserModel{
var id : String?
var lastLogin : String?
var name : String?
}
class parser : NSObject{
class func doParse(JSON: [String:Any?])->DataModel{
let dataModel = DataModel()
dataModel.total = JSON["total"] as? Int
dataModel.totalPages = JSON["totalPages"] as? Int
var userInfo : [UserModel] = []
let valueData : [String:String?]? = JSON["valueData"] as? [String:String?]
if let valueData = valueData{
for dataDict : [String:String?] in valueData{
let itemModel = UserModel()
itemModel.id = dataDict["id"] as? String
itemModel.lastLogin = dataDict["lastLogin"] as? String
itemModel.name = dataDict["name"] as? String
userInfo.append(itemModel)
}
dataModel.valueData = userInfo
}
return dataModel
}
}

class LoginData: NSObject {
let total: Int = 0
let totalPages: Int = 0
let valueData: Array<ValueData> = Array<ValueData>()
override init(total: Int!, totalPages: Int, valueData: Array<ValueData>!) {
self.total = total
self.totalPages = totalPages
self.valueData = valueData
}
}
struct ValueData {
let id: int?
let lastLogin: String?
let name: String?
init(id: int!, lastLogin: string, name: string!)
self.id = id ?? 0
self.lastLogin = lastLogin ?? ""
self.name = name ?? ""
}
}

you should use struct instead of class for creating model object...
advantages of struct over class refer
Why Choose Struct Over Class?
class/24232845
use two struct for holding your data one is for your single total count
and other is for last login detail
struct lastLogin {
let totalCount: (total: Int, totalPages: Int)
let valueArray: [lastLoginDetail]
}
struct lastLoginDetail {
let valueData: (id: String, lastLogin: String,name: String)
}
extension lastLoginDetail {
init(json: [String : String]) throws {
let id = json["id"]
let lastLogin = json["lastLogin"]
let name = json["name"]
let value = (id,lastLogin,name)
self.valueData = value as! (id: String, lastLogin: String, name: String)
}
}
extension lastLogin {
init(json: [String : Any]) throws {
let total = (json["total"] as! NSNumber).intValue
let totalPages = (json["totalPages"] as! NSNumber).intValue
let totalCounts = (total,totalPages)
var userInfo : [lastLoginDetail] = []
// Extract and validate valueData
let valueData = json["valueData"] as? NSArray
if let valueData = valueData{
for dataDict in valueData{
let dic : [String : String] = dataDict as! [String : String]
let lastLoginDeatails = try! lastLoginDetail(json: dic)
userInfo.append(lastLoginDeatails)
}
}
self.valueArray = userInfo
self.totalCount = totalCounts
}
}
func HowToUseModelClass(){
let jsonDic = NSDictionary()
// jsonDic // your json value
let dataValue = try! lastLogin(json: (jsonDic as! [String : Any])) // parsing the data
print(dataValue.totalCount.total)
print(dataValue.totalCount.totalPages)
print(dataValue.valueArray[0].valueData.id)
}

Related

How to create a generic array with multiple models in Swift?

I am trying to create a generic array with different models. I have a parser method like that. But it doesn't work because it returns [Any] and it's not typesafe. I need to access my Movie and CastMember objects after parse method. I will use this array in my tableviewcontroller delegate methods. How can I do that?
static func parseSearchResult(_ data:Dictionary<String, AnyObject>) -> [Any] {
var array = [Any]()
let jsonData = JSON(data)
if let resultData = jsonData["results"].arrayObject {
let result = resultData as! [[String:AnyObject]]
for element in result {
if((element["media_type"]?.isEqual("person"))!){
let person = CastMember(json: element)
array.append(person)
}
else if((element["media_type"]?.isEqual("movie"))!){
let movie = Movie(json: element)
array.append(movie)
}
}
}
return array
}
and these are my structs
struct CastMember{
var id : Int?
var originalName : String?
var castName : String?
var picturePath : String?
init(json: [String:Any]){
originalName = json["name"] as? String
id = json["id"] as? Int
castName = json["character"] as? String
picturePath = "https://image.tmdb.org/t/p/w200/"
picturePath?.append((json["profile_path"] as? String) ?? "")
}
}
struct Movie{
var id : Int?
var title : String?
var imagePath : String?
init(json: [String:Any]){
title = json["title"] as? String
id = json["id"] as? Int
imagePath = "https://image.tmdb.org/t/p/w200/"
imagePath?.append((json["poster_path"] as? String)!)
}
}
Make your Movie and CastMember classes confirm to the protocol Codable.
also you will have to write a struct or class which matches the response data , like it must have an array of results and any other key coming in response.
struct ResponseModel<T> : Codable {
let results : [T]
}
then decode it like this :
let response : ResponseModel = JSONDecoder.decode(T.self)
You should make a protocol
Example:
enum MediaType {
case movie, castMember
}
protocol SearchResult {
var title: String { get }
var mediaType: MediaType { get }
}
struct SearchResultViewModel: SearchResult {
let title: String
let mediaType: MediaType
init(title: String, mediaType: MediaType) {
self.title = title
self.mediaType = mediaType
}
}
Then your parseSearchResult should return an array of [SearchResult] objects that conforms to the protocol, in this case, an array of SearchResultViewModel

How to make model class for dictionary inside array in swift?

Here is my Json:
[ {
"id": 6854,
"name": "Laundry Iron",
"images": [
{
"id": 6856,
"src": "https://abcd.com/yzx/uploads/1750.jpg",
}
],
} ]
how do we make model class for getting "images":["src": "String" ]?. I want to grab "src" I have tried doing like , but it is not working :
class ProductModel {
var title: String?
var regularPrice: Int?
var salePrice: Int?
var productDescroption: String?
var imageUrl: [ImageUrl]?
init(productJsonL: NSDictionary) {
self.title = productJsonL["name"] as? String
self.regularPrice = productJsonL["regular_price"] as? Int
self.salePrice = productJsonL["sale_price"] as? Int
self.productDescroption = productJsonL["description"] as? String
//The problem is here ........
//self.imageUrl = productJsonL["images"]![0]!["src"] as? String
self.imageUrl = ImageUrl(imageUrlJson: (productJsonL["images"]![0] as? NSDictionary)!)
}
}
class ImageUrl {
var source: String?
init(imageUrlJson: NSDictionary) {
self.source = imageUrlJson["src"] as? String
}
}
please correct me with the structure like I have done above so that i can append everything at once in an array ? Thank you in advance!!
You need Codable
struct Root: Codable {
let id: Int
let name: String
let images: [Image]
}
struct Image: Codable {
let id: Int
let src: String // or let src: URL
}
do {
let res = try JSONDecoder().decode([Root].self, from: data)
print(res)
}
catch {
print(error)
}

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
}

Error "InvalidFirebaseData" while sending Data to Firebase

*** Terminating app due to uncaught exception 'InvalidFirebaseData', reason: '(setValue:) Cannot store object of type DemoApp.SendMessageInput at . Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.
This is how I'm trying to send.
Above error does not occur if I use normal dictionary to pass data but I want to do it via Model as one or more time I can simply call that model for input rather than dic os if changes happens in future then I have make changes in one file only.
let messageItem = SendMessageInput(content: text, displayName: currrentUser.name, fileLength: 0, fileUrl: "", fromID: currrentUser.userId, isRead: false, messageStatus: 0, messageType: MessageType.text.rawValue, timestamp: Timestamp, toID: self.toUserID)
itemRef.setValue(messageItem)
SendMessageInput.swift
class SendMessageInput: Reflect{
var content: String = ""
var displayName: String = ""
var fileLength: Int = 0
var fileUrl: String = ""
var fromID: String = ""
var isRead: Bool = false
var messageStatus: Int = 0
var messageType: String = ""
var timestamp: Int = 0
var toID: String = ""
var values:[String: AnyObject]{
let arrayValue: [Any] = [content, displayName, fileLength, fileUrl, fromID, isRead, messageStatus, messageType, timestamp, toID]
var dic: [String: AnyObject] = [:]
for index in 0..<self.getProperties().count{
dic[self.getProperties()[index]] = arrayValue[index] as AnyObject?
}
return dic
}
init(content: String, displayName: String, fileLength: Int, fileUrl: String, fromID: String, isRead: Bool, messageStatus: Int, messageType: String, timestamp: Int, toID: String) {
self.displayName = displayName
self.fileLength = fileLength
self.fileUrl = fileUrl
self.fromID = fromID
self.isRead = isRead
self.messageStatus = messageStatus
self.messageType = messageType
self.timestamp = timestamp
self.toID = toID
}
}
Reflect.swift
class Reflect: NSObject {
func getProperties()->[String] {
let mirror = Mirror(reflecting: self)
var properties = [String]()
for childern in mirror.children {
if childern.label == "super"{continue}
properties.append(childern.label!)
}
return properties
}
func getClassName() -> String {
let mirror = Mirror(reflecting: self)
return String(describing: mirror.subjectType)
}
}
You can't call .setValue() on arbitrary objects.
It looks like you're forgetting to call .values on your object. Try this
itemRef.setValue(messageItem.values)

How to know if the append function is filling the properties of a NSObject

Swift 2.0 Alamofire 2.0 Xcode 7 IOS 9
I have the next function which calls the API and retrieves the friend list in JSON format, convert the list in dictionary and append it to the Friendship NSObject
func GetFriends(completionHandler: ([FriendShip]?, NSError?) -> ()) {
Alamofire.request(Router.GetFriends(Test().getUserId())).responseJSON { (_, _, result) in
var friends = [FriendShip]()
switch result {
case .Success(let json):
if let responseObject = json as? [String: AnyObject], let hits = responseObject["hits"] as? [[String: AnyObject]] {
for dictionary in hits {
friends.append(FriendShip(dictionary: dictionary))
print(friends)
}
completionHandler(friends, nil)
}
case .Failure(_, let error):
completionHandler(nil, error as NSError)
}
}
the result of print(dictionary) is :
["_id": 546a6ef98e6df97032266, "friend": {
"_id" = 546a4b3e1f8d2c2630dd2;
name = "Daniela";
profileImageUrl = "https://api-static/profile/546a4b3e1f8d2c2630dd2.1.jpg";
statusTxt = "";
}]
["_id": 546a6f988e6df97032266, "friend": {
"_id" = 546a4ba51f8d2c2630dd2;
name = "Mara";
profileImageUrl = "https://api-static/profile/546a4ba51f8d2c2630dd2.1.jpg";
statusTxt = undefined;
}]
["_id": 546a70a18e6df97032266, "friend": {
"_id" = 546a4bd61f8d2c2630dd2;
name = "Alejandro";
profileImageUrl = "https://api-static/profile/546a4bd61f8d2c2630dd2.1.jpg";
statusTxt = "Marty";
}]
["_id": 546a715d8e6df97032266, "friend": {
"_id" = 546a4be01f8d2c2630dd2;
name = "Pedro";
profileImageUrl = "https://api-static/profile/546a4be01f8d2c2630dd1.1.jpg";
}]
classes Friendship and User
class FriendShip: NSObject{
var id: String?
var userId: String?
var user: User?
var friendId: String?
var friend: User?
var date: NSDate?
init(dictionary: [String: AnyObject]){
id = dictionary["id"] as? String
userId = dictionary["userId"] as? String
user = dictionary["user"] as? User
friendId = dictionary["friendId"] as? String
friend = dictionary["friend"] as? User
date = dictionary["date"] as? NSDate
}
override var description : String {
let friendString = friend!.name != nil ? friend!.name! : "nil"
let urlString = friend!.profileImageUrl != nil ? friend!.profileImageUrl! : "nil"
return "Friendship:\nfriend = \(friendString),\nurlString = \(urlString)"
}
}
class User: NSObject{
var id: String?
var name: String?
var statusTxt: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]){
id = dictionary["id"] as? String
name = dictionary["name"] as? String
statusTxt = dictionary["statusTxt"] as? String
profileImageUrl = dictionary["profileImageUrl"] as? String
}
override var description : String {
let nameString = name != nil ? name! : "nil"
let profileImageUrlString = profileImageUrl != nil ? profileImageUrl! : "nil"
return "Friendship:\nname = \(nameString),\nprofileImageUrl = \(profileImageUrlString)"
}
}
How can I know/view/print if the friends.append function is working properly and its filling OK all the properties of the Friendship NSObject?
If I understand you correctly you need a way to print / debug the contents of your Friendship class.
NSObject implements the NSObjectProtocol which has a computed property description. That property returns a String that represents the contents of the class. It is the text you see when you print an object. So your Friendship class already inherits this description protocol from NSObject. But in your case it only prints the class name, because that is the default implementation.
So if you want to have a more meaningful description of your class, you have to override the description property:
class Friendship: NSObject {
var name: String?
var age: Int?
override var description : String {
let nameString = name != nil ? name! : "nil"
let ageString = age != nil ? String(age!) : "nil"
return "Friendship:\nname = \(nameString),\nage = \(ageString)"
}
}
I don't know what your actual Friendship class looks like, so I made my own, very simple class that only contains a name and an age.
So now when you have 2 instances of Friendship (one filled and one empty) and you print them, you can see the contents of that objects:
let friendship1 = Friendship()
friendship1.name = "John"
friendship1.age = 34
print(friendship1)
let friendship2 = Friendship()
print(friendship2)
Prints:
Friendship:
name = John,
age = 34
Friendship:
name = nil,
age = nil
UPDATE
In your case the overridden description vars should look like this:
class FriendShip: NSObject{
var id: String?
var userId: String?
var user: User?
var friendId: String?
var friend: User?
var date: NSDate?
init(dictionary: [String: AnyObject]){
id = dictionary["id"] as? String
userId = dictionary["userId"] as? String
user = dictionary["user"] as? User
friendId = dictionary["friendId"] as? String
friend = dictionary["friend"] as? User
date = dictionary["date"] as? NSDate
}
override var description : String {
let friendDescription = friend != nil ? friend!.description : "nil"
let userIdString = userId != nil ? userId! : "nil"
return "Friendship:\nfriend = \(friendDescription),\nuserId = \(userIdString)"
}
}
class User: NSObject{
var id: String?
var name: String?
var statusTxt: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]) {
id = dictionary["id"] as? String
name = dictionary["name"] as? String
statusTxt = dictionary["statusTxt"] as? String
profileImageUrl = dictionary["profileImageUrl"] as? String
}
override var description : String {
let nameString = name != nil ? name! : "nil"
let profileImageUrlString = profileImageUrl != nil ? profileImageUrl! : "nil"
return "User:\nname = \(nameString),\nprofileImageUrl = \(profileImageUrlString)"
}
}
You can test it:
// test empty object
let friendship = FriendShip(dictionary: [String: AnyObject]())
print(friendship.description)
// test correct object
let user = User(dictionary: ["name": "John", "profileImageUrl": "http://image.com"])
let friendship2 = FriendShip(dictionary: ["friend": user, "userId": "1"])
print(friendship2.description)
Prints
Friendship:
friend = nil,
userId = nil
Friendship:
friend = User:
name = John,
profileImageUrl = http://image.com,
userId = 1

Resources