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