Convert Swift Dictionary With Enum Value To NSDictionary - ios

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

Related

Swift: More elegant way to write this array mapping?

I would like to know if there is a more elegant way to write this:
struct S {
var state: [String: Any]
public var amounts: [Amount] {
var result: [Amount] = []
(self.state["amounts"] as? [Any]?)??.forEach({ a in
result.append(Amount(a))
})
return result
}
}
struct Amount {
init(_ any: Any?) {}
}
I have tried using map for array, but I can't find a way to do so.
You could also use guard let and early return, which would make it look bit better.
Here is how I did it,
struct S {
var state: [String: Any]
public var amounts: [Amount] {
guard let amounts = state["amounts"] as? [Any] else {
return []
}
return amounts.map(Amount.init)
}
}
struct S {
var state: [String: Any]
public var amounts: [Amount] {
return (self.state["amounts"] as? [Any] ?? []).map({ Amount($0) })
}
}
struct Amount {
init(_ any: Any?) {}
}
You are using too many unnecessary optionals here. You should always use as? with a non-optional type. And instead of forEach, use map:
public var amounts: [Amount] {
if let anyArray = self.state["amounts"] as? [Any] {
return anyArray.map(Amount.init)
} else {
return []
}
}
You can get that working in a single line,
public var amounts: [Amount] {
return (self.state["amounts"] as? [Any])?.map({ Amount($0) }) ?? []
}
init(_ state: [Amount]) {
self.init()
guard let amounts = state["amounts"] as? [Any] else {
return []
}
return amounts.map(Amount.init)
}

How to map dictionary with dynamic keys with ObjectMapper?

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

String initializer for optional primitive types: Int? Double? Float? etc

Is it possible to have an extension for all of the above types without specifying each type individually?
For example, here is such extension for Double:
extension String {
init?(_ value: Double?) {
if let nonOpt = value {
self.init(nonOpt)
} else {
return nil
}
}
}
let nonOpt: Double = 1
let opt: Double? = 1
let string = String(opt)
print(string)
I'd like to allow string initialization with optional type if it is possible to initialize string with the original type.
Do you mean something like this
extension String {
init?<T : CustomStringConvertible>(_ value : T?) {
guard let value = value else { return nil }
self.init(describing: value)
}
}
or
extension String {
init?<T : LosslessStringConvertible>(_ value : T?) {
guard let value = value else { return nil }
self.init(value)
}
}
Rather than declaring an custom initializer, just use map to map the value to a String as such:
let optDouble: Double? = nil
let optionalDoubleString = opt.map { String($0) }
let optInt: Int? = nil
let optionalIntString = opt.map { String($0) }

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

How to loop over struct properties in Swift?

Is it possible to iterate over properties of a struct in Swift?
I need to register cells-reuse identifiers in a view controller that makes use of many different cell types (cells are organized in different nib files). So my idea was to put all reuse identifiers and the corresponding nib-files as static tuple-properties (reuseID, nibName) in a struct. But how can I iterate over all of them to register the cells with the tableView?
I already tried something (see my answer below). But is there a more easy way to do this, e.g. without putting every property inside an array?
Although old question, with Swift evolving this question has new answer. I think that you approach is way better for the described situation, however original question was how to iterate over struct properties, so here is my answer(works both for classes and structs)
You can use Mirror Structure Reference. The point is that after calling reflect to some object you get it's "mirror" which is pretty sparingly however still useful reflection.
So we could easily declare following protocol, where key is the name of the property and value is the actual value:
protocol PropertyLoopable
{
func allProperties() throws -> [String: Any]
}
Of course we should make use of new protocol extensions to provide default implementation for this protocol:
extension PropertyLoopable
{
func allProperties() throws -> [String: Any] {
var result: [String: Any] = [:]
let mirror = Mirror(reflecting: self)
guard let style = mirror.displayStyle where style == .Struct || style == .Class else {
//throw some error
throw NSError(domain: "hris.to", code: 777, userInfo: nil)
}
for (labelMaybe, valueMaybe) in mirror.children {
guard let label = labelMaybe else {
continue
}
result[label] = valueMaybe
}
return result
}
}
So now we can loop over the properties of any class or struct with this method. We just have to mark the class as PropertyLoopable.
In order to keep things static(as in the example) I will add also a singleton:
struct ReuseID: PropertyLoopable {
static let instance: ReuseID = ReuseID()
}
Whether singleton used or not, we could finally loop over the properties like follows:
do {
print(try ReuseID.instance.allProperties())
} catch _ {
}
And that's it with looping struct properties. Enjoy swift ;)
Using hris.to's awesome answer, I wanted to provide a Swift 3 answer that's more to the point and doesn't use singletons.
Protocol & Extension:
protocol Loopable {
func allProperties() throws -> [String: Any]
}
extension Loopable {
func allProperties() throws -> [String: Any] {
var result: [String: Any] = [:]
let mirror = Mirror(reflecting: self)
// Optional check to make sure we're iterating over a struct or class
guard let style = mirror.displayStyle, style == .struct || style == .class else {
throw NSError()
}
for (property, value) in mirror.children {
guard let property = property else {
continue
}
result[property] = value
}
return result
}
}
Example:
struct Person: Loopable {
var name: String
var age: Int
}
var bob = Person(name: "bob", age: 20)
print(try bob.allProperties())
// prints: ["name": "bob", "age": 20]
Now there's a much easier way to do this:
1: Create an Encodable protocol extension:
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
2: Make your struct/class conform to Encodable protocol
struct MyStruct: Encodable {...}
class MyClass: Encodable {...}
And then you can get a Dictionary representing your struct/class instance at any time:
var a: MyStruct
var b: MyClass
print(a.dictionary)
print(b.dictionary)
And then you can loop through the keys:
for (key, value) in a.dictionary { ... }
for (key, value) in b.dictionary { ... }
I made a recursive function based on #John R Perry's solution that goes deeper into properties that are objects. It also takes an parameter to limit how many levels deep it goes (default is Int.max) to help prevent stackoverflow's:
protocol Loopable {
func allProperties(limit: Int) [String: Any]
}
extension Loopable {
func allProperties(limit: Int = Int.max) [String: Any] {
return props(obj: self, count: 0, limit: limit)
}
private func props(obj: Any, count: Int, limit: Int) -> [String: Any] {
let mirror = Mirror(reflecting: obj)
var result: [String: Any] = [:]
for (prop, val) in mirror.children {
guard let prop = prop else { continue }
if limit == count {
result[prop] = val
} else {
let subResult = props(obj: val, count: count + 1, limit: limit)
result[prop] = subResult.count == 0 ? val : subResult
}
}
return result
}
}
I got rid of the check for if the object is a class or struct because that the parameter not being a class or struct is the base case of the recursive function, and it was easier to handle it manually than with errors.
Testing it:
class C {
var w = 14
}
class B: Loopable {
var x = 12
var y = "BHello"
var z = C()
static func test() -> String {
return "Test"
}
}
class A: Loopable {
var a = 1
var c = 10.0
var d = "AHello"
var e = true
var f = B()
var g = [1,2,3,4]
var h: [String: Any] = ["A": 0, "B": "Dictionary"]
var i: Int?
}
print(A().allProperties())
prints:
["e": true, "g": [1, 2, 3, 4], "f": ["z": ["w": 14], "x": 12, "y": "BHello"], "h": ["A": 0, "B": "Dictionary"], "c": 10.0, "i": nil, "d": "AHello", "a": 1]
(Dictionaries are unordered, so if you get a different order, that's why)
Here is an example of iterating over struct properties (reuse identifiers of UITableViewCells and the corresponding NIB-names) using Swifts tuple feature. This is useful if you like organizing your cells in nib files and have a UIViewController that makes use of many different cell types.
struct ReuseID {
static let prepaidRechargeCreditCell = "PrepaidRechargeCreditCell"
static let threeTitledIconCell = "ThreeTitledIconCell"
static let usageCell = "UsageCell"
static let detailsCell = "DetailsCell"
static let phoneNumberCell = "PhoneNumberCell"
static let nibNamePrepaidRechargeCreditCell = "PrepaidRechargeCreditCell"
static let nibNameThreeTitledIconCell = "IconCellWith3Titles"
static let nibNameUsageCell = "ListElementRingViewCell"
static let nibNameDetailsCell = "ListElementStandardViewCell"
static let nibNamePhoneNumberCell = "PhoneNumberCell"
static let allValuesAndNibNames = [
(ReuseID.prepaidRechargeCreditCell, ReuseID.nibNamePrepaidRechargeCreditCell),
(ReuseID.threeTitledIconCell, ReuseID.nibNameThreeTitledIconCell),
(ReuseID.usageCell, ReuseID.nibNameUsageCell),
(ReuseID.detailsCell, ReuseID.nibNameDetailsCell),
(ReuseID.phoneNumberCell, ReuseID.nibNamePhoneNumberCell)]
}
With that struct it is easy to register all cell types using a for-loop:
for (reuseID, nibName) in ReuseID.allValuesAndNibNames {
if let xibPath = NSBundle.mainBundle().pathForResource(nibName, ofType: "nib") {
let fileName = xibPath.lastPathComponent.stringByDeletingPathExtension
self.tableView.registerNib(UINib(nibName: fileName, bundle: nil), forCellReuseIdentifier: reuseID)
} else {
assertionFailure("Didn't find prepaidRechargeCreditCell 👎")
}
}
Knowing that in Swift 1.2 you could use reflect(), and since Swift 2 you can use Mirror, here is an addition to hris.to's answer for Swift 3 and 4.
protocol Loopable {
var allProperties: [String: Any] { get }
}
extension Loopable {
var allProperties: [String: Any] {
var result = [String: Any]()
Mirror(reflecting: self).children.forEach { child in
if let property = child.label {
result[property] = child.value
}
}
return result
}
}
Usage on any struct or class:
extension NSString: Loopable {}
print("hello".allProperties)
// ["_core": Swift._StringCore(_baseAddress: Optional(0x00000001157ee000), _countAndFlags: 5, _owner: nil)]
struct IdentifiersModel : Codable {
let homeReuseID: String = "Home_Reuse_ID"
let offersReuseID: String = "Offers_Reuse_ID"
let testReuseID: String = "Test_Reuse_ID"
func toDic() -> [String:Any] {
var dict = [String:Any]()
let otherSelf = Mirror(reflecting: self)
for child in otherSelf.children {
if let key = child.label {
dict[key] = child.value
}
}
return dict
}
}
Create a new instance from the struct and call toDic() function
let identifiersModel = IdentifiersModel()
print(identifiersModel.toDic())
Output:
["offersReuseID": "Offers_Reuse_ID", "testReuseID": "Test_Reuse_ID", "homeReuseID": "Home_Reuse_ID"]
I found that RPatel99's answer worked the best for me, but it did not handle the case where there as an array of Loopables inside another Loopable.
Here is a version that fixes that issue.
protocol Loopable {
func allProperties(limit: Int) -> [String: Any]
}
extension Loopable {
func allProperties(limit: Int = Int.max) -> [String: Any] {
return props(obj: self, count: 0, limit: limit)
}
private func props(obj: Any, count: Int, limit: Int) -> [String: Any] {
let mirror = Mirror(reflecting: obj)
var result: [String: Any] = [:]
for (property, value) in mirror.children {
var val = value
if let values = value as? [Loopable] {
var vals = [Any]()
for val in values {
vals.append(val.allProperties())
}
val = vals
}
guard let prop = property else { continue }
if limit == count {
result[prop] = val
} else {
let subResult = props(obj: val, count: count + 1, limit: limit)
result[prop] = subResult.count == 0 ? val : subResult
}
}
return result
}
}
class C {
var w = 14
}
class B: Loopable {
var x = 12
var y = "BHello"
var z = C()
static func test() -> String {
return "Test"
}
}
class A: Loopable {
var a = 1
var c = 10.0
var d = "AHello"
var e = B()
var f = [B(), B(), B()]
var g = [1,2,3,4]
var h: [String: Any] = ["A": 0, "B": B().allProperties()]
var i: Int?
}
print(A().allProperties())
The result I get is this
["h": ["A": 0, "B": ["z": ["w": 14], "y": "BHello", "x": 12]], "e": ["y": "BHello", "z": ["w": 14], "x": 12], "f": [["y": "BHello", "z": ["w": 14], "x": 12], ["x": 12, "z": ["w": 14], "y": "BHello"], ["y": "BHello", "x": 12, "z": ["w": 14]]], "i": nil, "g": [1, 2, 3, 4], "a": 1, "d": "AHello", "c": 10.0]
I hope someone else will find this useful.
This result could probably also be achieved by something like this

Resources