last day (14 days later) » 

5:50 PM
2
A: Swift: Alternatives to super class methods to generate objects

AlexanderHere is how I would write this code in idiomatic Swift: struct Book { let title: String let author: String let description: String /* an implicit member wise initializer is generated, which would otherwise look something like this: init(title: String, author: String, des...

 
@Alexander, This is great but in your case you don't use class func. Can I ask why?
 
@appzYourLife 1) [Book]? would entail you entirely give up just because one book (out of potentially many) gave up. I think it's better to salvage as many as possible, and let the consumer deal with the nil case. 2) Oh right, I can let the inference engine do the work!
@user2924482 Class funcs and static funcs are similar in that they're both called on a type. Class functions can be overridden by subclasses, whereas static functions can't. Since structs can't be subclassed, class functions on structs wouldn't be of any use, thus, class functions can only be declared on Classes, whereas static functions are applicable to classes, structs, protocols and even enums.
@PauloMattos Oh wow, I just learned a really cool thing. Originally, all this code was in the struct declaration. In that case, the existence of init?(fromDict) would cause the implicit memberwise initializer to not be generated. I eventually split it up into a separate struct declaration with a complimentary extension. It turns out that inits found in extensions won't prevent the formation of the implicit memberwise initializer!
@PauloMattos Discovering little nuggets of knowledge like that are my primary reason for being involved with SO. It's great!
 
@Alexander, this is great and I'm trying to use it calling with this code: let inventoryBooks = Book.books(fromDictArray: json .object(forKey: "books") as! [[String : String]] ) but the is crashing because the object is NSArray. How I modify static func books to take NSArray ?
 
@user2924482 NSArray probably isnt the issue. I suspect your array's contents aren't strictly NSDictionaries mapping strings to only strings. Can you post what your data structure looks like?
 
@Alexander, I have updated my original post. Please let me know if you have any ideas of what is going on
 
5:50 PM
@user2924482 you havent satisfied my question "Can you post what your data structure look like?". Its an NSArray, okay, what does it contain? Dictionaries? What are the key types of the dictionaries? The values? Give us a real sample to work with
 
@Alexander, I post a sample of what the json looks like
 
As I expected, your array of dictionaries isn't purely string keys and string values
because the id is an int
 
What I post it was just one node of the Json
 
hold on
When you're trying to cast your NSArray to [[String: String]] (a.k.a Array<Dictionary<String, String>>), the cast is failing because the NSArray actually contains dictionaries with String keys, but all kinds of values (strings, ints, etc.)
so cast instead to [[String: Any]]
and don't force cast (as!). Cast safely and handle the errors
You don't want your app to crash just because it was served ill-formed JSON
I have an exam to worry about for the next few hours, so ping me later if you need help, @user2924482
 
6:08 PM
Let me give it a try
 
6:21 PM
see my edited answer
 
This made the trick books(fromDictArray array: [[String: Any]])
I really appreciate your help with this
 
yep, that's what my answer shows :p
try to avoid NSArray and NSDict and similar foundation data structures, as much as you can, in Swift
bridge them into native, type safe Swift equivalents as soon as possible
 
6:49 PM
I'll try Old habits die hard :S coming from ObjC-land. But I'll try to switch to swift native types. Once again thank you for your help.
 

last day (14 days later) »