I'm new to ObjectMapper. I have response from the server:
{
"123123": 10,
"435555": 2,
"435333": 8,
"567567": 4
}
Keys (dynamic) are going to be IDs. Values are going to be COUNT. How can I map it with ObjectMapper?
My code isn't working because dynamic keys:
extension Item: Mappable {
private static let kId = "id"
private static let kCount = "count"
public init?(map: Map) {
self.init()
}
mutating public func mapping(map: Map) {
id <- map[Item.kId]
count <- map[Item.kCount]
}
}
Your response is an object and you can access it via map.JSON, and its type is [String: Any]. Then you can use that like a normal Dictionary.
Here I create a class named Model that has array of items (of type Item) and in func mapping(:Map) I mapped map.JSON elements to Item.
class Model: Mappable {
typealias Item = (id: String, count: Int)
var items: [Item] = []
required init?(map: Map) {
}
func mapping(map: Map) {
let rawDictionary = map.JSON
let items = rawDictionary.compactMap { (key, value) -> Item? in
guard let intValue = value as? Int else { return nil }
return (key, intValue)
}
self.items = items
}
}
let jsonString = """
{
"123123": 10,
"435555": 2,
"435333": 8,
"567567": 4
}
"""
let model = Model(JSONString: jsonString)
print(model?.items[0]) //Optional((id: "123123", count: 10))
You can try
do{
let res = try JSONDecoder().decode([String:Int].self, from: data)
}
catch {
print(error)
}
Related
I am new in swift programing. I'm trying to learn plist file. I have a plist file which has data of Country, State and city. I want to iterate through the data of plist file. But I can't understand how to create an array or dictionary to store the data for country, state and City. Can you help me with how to manipulate the following data of plist file.
{
Country = (
{
CountryName = India;
State = (
{
City = (
Ahmedabad,
Vadodara,
Surat,
Aanand,
Bharuch
);
StateName = Gujrat;
},
{
City = (
Mumbai,
Pune,
Nagpur,
Nasik,
Thane
);
StateName = Maharastra;
},
{
City = (
Kochi,
Kanpur,
Alleppey,
Thrissur,
Thiruvananthapuram
);
StateName = Kerala;
}
);
},
// swift 3 using structure. Manually parsing of data.
struct ListData {
var countries : [Country]?
init(dict:[String:AnyObject]) {
if let countryDict = dict["Country"] as? [[String:AnyObject]] {
self.countries = parseArray(dictArray: countryDict)
}
}
func parseArray(dictArray:[[String:AnyObject]]) -> [Country] {
var array = [Country]()
for dict in dictArray {
let country = Country(dict: dict)
array.append(country)
}
return array
}
}
struct Country {
var countryName : String?
var states : [State]?
init(dict:[String:AnyObject]) {
countryName = dict["CountryName"] as? String
if let stateDict = dict["State"] as? [[String:AnyObject]] {
states = parseArray(dictArray: stateDict)
}
}
func parseArray(dictArray:[[String:AnyObject]]) -> [State] {
var array = [State]()
for dict in dictArray {
let state = State(dict: dict)
array.append(state)
}
return array
}
}
struct State {
var stateName : String?
var cities : [String]?
init(dict:[String:AnyObject]) {
self.stateName = dict["StateName"] as? String
if let cityDict = dict["City"] as? [AnyObject] {
cities = parseArray(dictArray: cityDict)
}
}
func parseArray(dictArray:[AnyObject]) -> [String] {
var array = [String]()
for dict in dictArray {
array.append(dict as! String)
}
return array
}
}
var listData : ListData? = nil
if let path = Bundle.main.path(forResource: "Property List" , ofType: "plist") {
let rootDict = NSDictionary(contentsOfFile: path) as? [String:AnyObject]
listData = ListData(dict: rootDict!)
}
// Using swift 4 and codable protocol.
struct ListData : Codable {
var countries : [Country]?
enum CodingKeys : String, CodingKey {
case countries = "Country"
}
}
struct Country : Codable {
var countryName : String?
var states : [State]?
enum CodingKeys : String, CodingKey {
case countryName = "CountryName"
case states = "State"
}
}
struct State : Codable {
var stateName : String?
var cities : [String]?
enum CodingKeys : String, CodingKey {
case stateName = "StateName"
case cities = "City"
}
}
var listData : ListData? = nil
if let url = Bundle.main.url(forResource: "Property List", withExtension: "plist") {
if let data = try? Data(contentsOf: url) {
let decoder = PropertyListDecoder()
do {
listData = try decoder.decode(ListData.self, from: data)
} catch (let err) {
print(err.localizedDescription)
}
}
}
You can use Object mapper
https://github.com/Hearst-DD/ObjectMapper
where, install the pod and then
create your class as
class State : Mappable {
var stateName: String?
var city: [String]?
required init?(map: Map) {}
func mapping(map: Map) {
stateName <- map["StateName"]
city <- map["City"]
}
}
class Country : Mappable {
var countryName: String?
var state: [State]?
required init?(map: Map) {}
func mapping(map: Map) {
countryName <- map["CountryName"]
state <- map["State"]
}
}
class CountryData: Mappable {
var countries: [Country]?
required init?(map: Map) {}
func mapping(map: Map) {
countries <- map["Country"]
}
}
get your plist content in a varibale like
var dictRoot: NSDictionary?
var countryItems: [String:AnyObject]?
if let path = Bundle.main.path(forResource: "MyCountryList" , ofType: "plist") {
dictRoot = NSDictionary(contentsOfFile: path)
}
if dictRoot != nil
{
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
countryItems = dictRoot as? [String:AnyObject]
tableView.reloadData()
}
let countryData = CountryData(JSON: countryItems)
now you can use it as you wish
I have a JSON which receives an array from an API call
Within that array are 3 other arrays:
userDetails, userStats, communities
An example of this API call is:
["communities": <__NSArrayI 0x6000002540a0>(
{
id = 5;
name = South;
},
{
id = 13;
name = HurraHarry;
},
{
id = 15;
name = EnclliffeT;
}
)
, "userStats": {
totalDraws = 3;
totalLosses = 10;
totalWins = 1;
}, "userDetails": {
id = 31;
"user_email" = "steve#gmail.com";
"user_name" = "Steve Base";
}]
I would like to store the array userStats in a variable that I can pass to another VC.
I have a global variable var userStatsArray = [AnyObject]() in my class
and the following code deals with the JSON:
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
self.communityIdsArray = arr.flatMap { $0["id"]!}
}
if let dict = json?["userDetails"] as? [String:String] {
self.tempPlayerId = [dict["id"]!]
let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
let newPlayerId = String(characterArray)
self.playerId = newPlayerId
}
if let tempArray = json?["userStats"] as? [String:AnyObject]{
print ("here ", tempArray)
}
The print command successfully prints the userStats array with all its headers (totalWins, totalDraws, totalLosses...) -
How do I store this array into my global variable var userStatsArray = [AnyObject]() so I can pass it to another VC?
Better you create one custom class like this, and declare the array with that custom class type. then you cast your userStats object to your custom class type.
class userStats: NSObject {
var totalDraws: NSNumber?
var totalLosses: NSNumber?
var totalWins: NSNumber?
init(totalDraws: NSNumber?, totalLosses: NSNumber?, totalWins: NSNumber?) {
self.totalDraws = totalDraws
self.totalWins = totalWins
self.totalLosses = totalLosses
}
}
var userStatsArray = [userStats]()
// CHANGE YOUR CODE LIKE THIS
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
self.communityIdsArray = arr.flatMap { $0["id"]!}
}
if let dict = json?["userDetails"] as? [String:String] {
self.tempPlayerId = [dict["id"]!]
let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
let newPlayerId = String(characterArray)
self.playerId = newPlayerId
}
if let tempArray = json?["userStats"]as? userStats {
userSytatsArray.append(tempArray)
}
Take a look at ObjectMapper! With that powerful framework you can create the mappable models of your data returned by the API and let it perform the whole work for you :)
Declare your model classes like this:
class UserInfo: Mappable {
var communities : [Community]?
var stats: UserStats?
var details: UserDetails?
required init?(map: Map) {
}
func mapping(map: Map) {
communities <- map["communities"]
stats <- map["userStats"]
details <- map["userDetails"]
}
}
class Community: Mappable {
var id: Int!
var name: String!
required init?(map: Map) {
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
}
}
class UserStats: Mappable {
var totalDraws : Int!
var totalLosses : Int!
var totalWins : Int!
required init?(map: Map) {
}
func mapping(map: Map) {
totalDraws <- map["totalDraws"]
totalLosses <- map["totalLosses"]
totalWins <- map["totalWins"]
}
}
class UserDetails: Mappable {
var id : Int!
var email : String!
var username : String!
required init?(map: Map) {
}
func mapping(map: Map) {
id <- map["id"]
email <- map["user_email"]
username <- map["user_name"]
}
}
And later just:
let user = UserInfo(JSONString: JSONString)
I am using the alamofire to get response JSON from server, then using ObjectMapper to map string to Realm object.
The realm object is:
class SolutionVideo: Object, Mappable {
dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""
override static func primaryKey() -> String? {
return "svID"
}
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
svID <- map["svID"]
solutionTitle <- map["solutionTitle"]
videoName <- map["videoName"]
relatedInfo <- map["relatedInfo"]
shortDesc <- map["shortDescription"]
}
}
The json string is:
[
{
"svID": "10",
"solutionTitle": "Video10",
"videoName": "Video10",
"realtedInfo": "",
"shortDescription": ""
},
{
"svID": "9",
"solutionTitle": "Video9",
"videoName": "Video9",
"realtedInfo": "",
"shortDescription": ""
}
]
in my viewController:
#IBAction func updateBtn(sender: AnyObject) {
// download file to update Realm
let url = "http://janicedemo.com/updates.json"
Alamofire.request(.GET, url).responseArray { (response: Response<[SolutionVideo], NSError>) in
let Array = response.result.value
print(Array)
if let Array = Array {
for video in Array {
let dbURL = Realm.Configuration.defaultConfiguration.fileURL
let realm = try! Realm(fileURL: dbURL!)
try! realm.write{
print("will save")
realm.add(video, update: true)
}
}
}
}
The problem is that I can add the object successfully. But the svID(primark key) keeps 0, instead of 10 or 9 (set in JSON). Is it because I have set default value to svID? Can someone gives me a hint? Thanks
Try
class SolutionVideo: Object, Mappable {
dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""
func setCompoundID(id: Int) {
self.svID = svID
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.solutionTitle = solutionTitle
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.videoName = videoName
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.relatedInfo = relatedInfo
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.shortDesc = shortDesc
compoundKey = compoundKeyValue()
}
dynamic lazy var compoundKey: String = self.compoundKeyValue()
override static func primaryKey() -> String? {
return “compoundKey”
}
func compoundKeyValue() -> String {
return "\(svID)\(solutionTitle)\(videoName)\(relatedInfo)\(shortDesc)”
}
}
The main thing I can think of is that the primary key values are coming down as strings instead of proper integers (i.e., "10" instead of simply 10). It's possible that the mapper isn't smart enough to handle conversion of strings to integers, so it's just defaulting to 0.
According to the ObjectMapper documentation, you should be able to perform this conversion inline in your mapping function:
svID <- (map["svID"], TransformOf<Int, String>(fromJSON: { Int($0!) }, toJSON: { $0.map { String($0) } }))
Also, as a sidenote, I noticed that one of your JSON key names has a spelling error: "realtedInfo". So I'd recommend double-checking that works too. :)
I have this JSON:
{
"location": {
"position": {
"type": "Point",
"coordinates": [
45.579553,
11.751805
]
}
}
}
Which belongs to another JSON object.
Trying to map it with Realm and ObjectMapper, I am findind difficulties mapping the coordinates property which is an array of double.
That's what reading the documentation and S.O. seems to have sense:
import Foundation
import RealmSwift
import ObjectMapper
class Coordinate:Object, Mappable{
dynamic var latitude:Double = 0.0
dynamic var longitude:Double = 0.0
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
latitude <- map[""]
longitude <- map[""]
}
}
class Position: Object, Mappable{
var type:String = ""
var coordinates:Coordinate?
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
type <- map["type"]
coordinates <- map["coordinates"]
}
}
class Location: Object, Mappable{
dynamic var id = ""
dynamic var position:Position?
dynamic var desc = ""
override static func indexedProperties()->[String]{
return["id"]
}
override class func primaryKey() -> String? {
return "id"
}
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
position <- map["position"]
}
}
However I'm stuck in understanding how to map the "coordinates" object. Please note that this problem has nothing to do with ObjectMapper itself, it's more of a question on how to assign an array of Double to a property in a Realm model.
I was able to solve this following the indications in this issue:
https://github.com/realm/realm-cocoa/issues/1120 (credits #jazz-mobility)
class DoubleObject:Object{
dynamic var value:Double = 0.0
}
class Position: Object, Mappable{
var type:String = ""
var coordinates = List<DoubleObject>()
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
type <- map["type"]
var coordinates:[Double]? = nil
coordinates <- map["coordinates"]
coordinates?.forEach { coordinate in
let c = DoubleObject()
c.value = coordinate
self.coordinates.append(c)
}
}
}
You can now simply use List<Double>() without it storing an object.
More information can be found here: https://academy.realm.io/posts/realm-list-new-superpowers-array-primitives/
#objcMembers class RealmObject: Object, Mappable {
dynamic var listValues = List<MyRealmObject>()
required convenience init?(map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
listValues <- (map["listValues"], RealmlistObjectTransform())
}
}
#objcMembers class MyRealmObject: Object, Mappable {
required convenience init?(map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
}
}
class RealmlistObjectTransform: TransformType {
typealias Object = List<MyRealmObject> // My Realm Object here
typealias JSON = [[String: Any]] // Dictionary here
func transformFromJSON(_ value: Any?) -> List<MyRealmObject>? {
let list = List<MyRealmObject>()
if let actors = value as? [[String: Any]] {
let objects = Array<MyRealmObject>(JSONArray: actors)
list.append(objectsIn: objects)
}
return list
}
func transformToJSON(_ value: List<MyRealmObject>?) -> [[String: Any]]? {
if let actors = value?.sorted(byKeyPath: "").toArray(ofType: MyRealmObject.self).toJSON() {
return actors
}
return nil
}
}
If I have a dictionary with the type Dictionary<String, MyEnum>, I cannot figure out how to convert it to an NSDictionary so I can serialiaze it to JSON via NSJSONSerialize.dataWithJSONObject.
The compiler tells me that "Dictionary<String, MyEnum> is not convertible to NSDictionary". Do I need to create a new dictionary with the string values of the enum a la
var newDict = Dictionary<String, String> (or <String, AnyObject>);
for (key, val) in oldDict {
newDict[key] = val;
}
or is there a better way?
NSJSONSerialize and friends can only deal with small subset of NSObject children (NSNumber, NSString, NSArray, NSDictionary, and NSNull) that correspond to the JSON native data types. In addition, a Dictionary can only be converted to an NSDictionary if both the key and value are NSObject or inherently convertible to NSObject (String and numeric types).
In order to serialize your Dictionary you'll need to convert the Enum to one of those NSJSONSerialize data-types, similar to your example:
enum MyEnum : String {
case one = "one"
case two = "two"
}
let dict = ["one":MyEnum.one, "two":MyEnum.two]
var newDict = Dictionary<String, String>()
for (key, val) in dict {
newDict[key] = val.rawValue
}
let data = NSJSONSerialization.dataWithJSONObject(newDict, options: .allZeros, error: nil)
As an alternative, since this kind of manipulation is fairly common, you might want to consider adding this category to Dictionary, which gives it a convenient map function:
extension Dictionary {
init(_ pairs: [Element]) {
self.init()
for (k, v) in pairs {
self[k] = v
}
}
func map<K: Hashable, V>(transform: Element -> (K, V)) -> [K: V] {
return Dictionary<K, V>(Swift.map(self, transform))
}
}
Once that's done, the conversion is simply:
let mapped = dict.map { (key, value) in (key, value.rawValue) }
public protocol EnumCollection: Hashable {
static func cases() -> AnySequence<Self>
static var allValues: [Self] { get }
}
public extension EnumCollection {
public static func cases() -> AnySequence<Self> {
return AnySequence { () -> AnyIterator<Self> in
var raw = 0
return AnyIterator {
let current: Self = withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: self, capacity: 1) { $0.pointee } }
guard current.hashValue == raw else {
return nil
}
raw += 1
return current
}
}
}
public static var allValues: [Self] {
return Array(self.cases())
}
}
public enum ScreenName: String, EnumCollection
{
case HomeView = "Home View"
case SignUpViewController = "Domain Validate View"
var name: String {
get { return String(describing: self) }
}
var description: String {
get { return String (self.rawValue) }
}
// a dictionary mapping the raw values to the values
static var allValuesDictionary: [String : String]{
var values = [String : String]()
let mirror = Mirror(reflecting: ScreenName.allValues)
for (_, v) in mirror.children{
guard let screenName = v as? ScreenName else{continue}
values[screenName.name] = screenName.description
}
return values
}
}
Now you can call the method allValuesDictionary will return the dictionary with <key, value> = <case, rawValue>
<HomeView, "Home View">, <SignUpViewController, "Domain Validate View">