last day (14 days later) » 

7:05 PM
1
A: More appropriate solution than a nested dictionary?

Eric DYou could use structs instead of dictionaries. Quick example, let's make a Bus object: struct Bus { var name: String var direction: String var times: [NSDate] } It holds the values for each bus: name of the bus, its direction, and an array of dates for the bus times. Of course it...

 
How would you get the times for a specific name of a bus? I would need to search for the bus and then print out the items in the array?
 
I've updated my answer with an example. Note that I'm using map and filter for my examples but if you're not familiar with this you can also use loops as usual of course.
 
At the moment I have a class with the stops and a class with the derpartures, which is contained in the stop class in an array. So you would just make another class/struct and loop through all the departures to make sure they get added to the correct bus-object? The things is, that the same bus will be repeated numerous of times in the JSON, since it has several departures a day. So I guess I'll have to loop through my list of busses and check if the one I'm at is already in there, and If not I'll create an object and add it, otherwise I'll just add the time to the list of the current object?
 
It's up to you to decide how you structure your classes (or structs) but I would strongly recommend not using nested dictionaries. It's very cumbersome and not type safe - on the other hand, well-structured objects and convenient wrappers around them (a BusManager can have an array of Bus objects and methods to sort them, for example) are life savers when using and filtering collections like you're doing. IMO, of course - there's other patterns, but this is a rather usual one.
 
I see. Just another question, when I go through the list to check if an element is already there I use list.contains(item) however, when the element is contained I want to add the item time to the current items array, how is this possible?
 
7:05 PM
You can use indexOf. This could help: stackoverflow.com/a/32953118/2227743 Once you get the index, you can subscript the array to get the object, then you add it where you want.
 
Thanks! I want to compare the elements from the two different list on their name, how can I do this? I got two different object, a Departure-object and a BusDeparture-Object. So I guess I need to specify, in my contains, what they should compare?
 
You're welcome. The example I just linked does that exactly (you just adapt to your own types). // You can also just compare their properties directly in various methods instead of making everything Equatable - your choice, depends what you need/prefer.
 
And I would just compare their names instead of the hashvalues since the objects are different, right?
 
Yes, exactly like in the first part of the linked example. The second part is more about comparing objects by a computed uniqueness instead of just a property like the name.
 
And where would I put that function when I'm comparing two different objects?
 
7:05 PM
I can't decide for you my friend. It can be in the Bus struct itself, or in a Manager class, you decide how you architecture all this, you decide what makes more sense. It depends on so many things. Open a Playground and go experiment now, this question is resolved. ;) If you have new questions, don't hesitate but make a new post, it's better. You can point to this one in the new one if necessary.
 
The compiler doesn't seem to like the func ==(lhs: Meme, rhs: Meme) -> Bool {
return lhs.name == rhs.name
} Is it an older version of Swift?
 
Show me your version where you replaced Meme with your own object, maybe you made a typo or something
The linked example works for Swift 2 (and 3).
 
I've placed it in my Bus class, func ==(departure: Departure) -> Bool { return self.name == departure.name }
 
it has to be at the root of the Swift file
it's the rule for overriden operators
and it has to compare 2 values
otherwise it's not an == operator anymore :)
in A == B, lhs is the left value, A, and rhs is the right value, B
 
So I would just do it like this in the root, and then provide both objects when I compare?
func ==(BusDeparture: BusDeparture, departure: Departure) -> Bool {
return BusDepartures.name == departure.name
}
BusDeparture = Bus btw
 
7:12 PM
yes but replace the named arguments with "lhs" and "rhs" if you want it to be conform to Equatable
lhs means left hand side, same for rhs: right hand side
then you can do if bus1 == bus1 { ... } without having to specify the name yourself
 
Oh I see. But I guess I can't use contains now? I'm looping through my departures, and I want to check if a BusDeparture with the name of the Departure is present in the list.
 
if it conforms to equatable
yes, conforming to Equatable guarantees that you can use contains, filter, indexOf, etc
 
Hmm, it seems like I can't have two different Class objects. In your example you got Meme at both lhs and rhs
 
if busses.contains(bus2) { ... } is possible thanks to Equatable, that's the whole point
 
Yea, It's like Comparable in C# and Java I guess ;o)
However I don't compare objects of the same type
 
7:17 PM
your objects would have to inherit from a class and you would compare the property of this class / or you can make your different types conform to a same protocol of your own
etc
There's a lot to learn but it's simple to follow. Experiment. ;)
 
Or maybe I could create BusDeparture objects of my Departures and then compare them? ;o)
 
You have an infinity of choices. It can be daunting. :D
 
I guess ;o) Just a quick one, when I try to implement Equatable is says "Redundant conformance of 'BusDeparture' to protocol 'Equatable'. How come?
I looks perfect similar to your example
*it
 
Without seeing your whole code I can't really know.
 
Oh, can it have something to do with me inheriting from NSObject?
 
7:22 PM
Yes. Remove it if you don't absolutely need it, many things will conflict otherwise, absolutely.
 
Now it says it doesn't conform, do I need to add anything other than the == operator?
Hold on
I was just a tard :D
Thanks for your time man! Really appreciated!
 
Bah - I don't believe that. Just a momentary lapse of focus probably. ;)
You're welcome.
 
It's taking some time to adapt to Swift from Java, however it seems like Swift is becoming increasingly popular ;o)
So worth to learn I guess :D
Do you know any great Swift communities online? Other than here?
 
I guess you could browse my twitter profile, I follow a lot of Swift personalities - most of them have excellent blogs, sites, etc. There's also (Erica Sadun, NSHipster, etc). But SO is my main source/target for Swift technicalities.
 
7:38 PM
Right, I've also been using Ray Wenderlich a lot ;o) Isn't there an easy way to append and array to an array in Swift? Like appendAll in other languages?
 
The target would have to have the right type, like: var all: [[Stuff]] then append an array of Stuff to the array of arrays: all.append(arrayOfStuff)
there's also appendContentsOf to append, well, the contents instead of the array itself, if the target is [Stuff] instead of [[Stuff]]
arrayOfStuff.appendContentsOf(otherArrayOfStuff)
 
Oh, is it bad practice to use the += operator?
 
and the like...
no it's ok
 
But thanks for the many opportunities, will def look into them!
 
You're welcome. Enjoy!
 

last day (14 days later) »