My first question on StackOverflow :)
I started to learn Swift in XCode a few days ago and I'm having some trouble understanding the value type in Swift. Since I'm quite familiar with Java, I'm just going to explain my question in Java. Here's what I want to write in Swift.
class Node {
public int value;
public List<Integer> children;
public Node parent;
public Node() {
value = 0;
children = new ArrayList<Integer>();
}
public Node bestChild() {
Node result = new Node();
for (child : this.children) {
if (child.value > result.value)
result = child;
}
return result;
}
}
For the sake of this question, let's not talk about private, public, or generic here. Just keep it simple! Here's my try in Swift.
public class Node {
var value: Int
var children: NSSet
var parent: Node?
public init() {
self.value = 0
children = NSSet()
}
public func bestChild() -> Node {
var result = Node()
for child in children {
if (child.value > result.value) { //error
result = child //error
}
}
return result
}
}
Both the condition for the if statement and the line inside have errors. XCode suggested casting the variables but still it didn't solve the errors.
if ((child as AnyObject).value > result.value) {
result = child as! Node
}
Since Java is the only language I know, if someone could explain it in term of Java I would greatly appreciate it. If not, please keep it simple so I can wrap my head around it :)
Thanks in advance!
welcome to Stack Overflow and Swift programming!
Improvements to the Java code
Firstly, I suggest some changes to the Java code.
childrenvariable is typeList<Integer>, but I suspect you meant it to beList<Node>.Nodejust for comparison's sake. With this approach, ifbestChild()returns aNodewith a value of 0, it can't be known if that's because the children list was empty, of if there really was aNodewith value 0 that was the highest value. I would use the stream API, though you could manually implement such functionality.Here is the end result:
The Swift equivalent
Here's how I would write this in Swift. Feel free to ask follow up questions
The Swift equivalent of the antiquated Java apporach
Here's the Swift implementation of the antiquated Java approach to the
bestChildmethod. This is just for comparison's sake. DON'T DO THIS!To address the issues in your solution attempt:
NSSetis toSwift, asHashSet(without the generic type parameter, notHashSet<T>) is to Java. It's a datatype from theFoundationlibrary, that was invented for Objective-C. You shouldn't be using it in Swift in 99.999% of cases.NSSetacts as a set ofAnyObject. In order to be able to call.valueon an object from the set, you need to first manually cast the object toNode.The solution to all that is to instead use
Set<Int>, which is Swift's nativeSetdata structure. Though this raises the question, why are you using a Set in the swift solution, but an array in the Java solution?