If I have a Swift dictionary initialized like this:
let dict = [String: Int]()
I can iterate through it like this:
XCode:
for item in dict {
let itemKey: String = item.key
let itemValue: Int = item.value
// Do something...
}
Fire/VS2017 (.2373) -> Android/Java/.NET
for item in dict {
let itemKey: String = item.Item1
let itemValue: Int = item.Item2
// Do something...
}
I understand there are differences here (key and value vs. Item1 and Item2) because tuples behave differently in Apple Swift and Silver.
However, I tried to unify the syntax so it would be the same on all platforms, like this:
for item: (key: String, value: Int) in dict {
let itemKey: String = item.key
let itemValue: Int = item.value
// Do something...
}
This compiles fine in XCode and Fire for Java, but it doesn’t compile in VS2017 for .NET because the compiler complains that it cannot cast Tuple<String, Int>
(dictionary item) as ValueTuple<String, Int>
(the (key: String, value: Int)
syntax). I think this is a tad inconsistent, given that Java code compiles fine, so it’s reasonable to expect that .NET code would, too. Then again, I’m not an expert on various “same looking class but possibly completely different behavior” that you guys have to take into account on different platforms, which is why probably the above stuff happens.
Anyway, is there any way we can have this:
for item in dict {
let itemKey: String = item.key
let itemValue: Int = item.value
// Do something...
}
or this:
for item (key: String, value: Int) in dict {
let itemKey: String = item.key
let itemValue: Int = item.value
// Do something...
}
compile in Fire/VS for both Java (it already does) and .NET (it doesn’t at the time)? Right now it’s a bit of maintenance overkill because I have two different files (Cocoa-Java and .NET) with protocol extensions to access these values, which means that for a particular business logic that could otherwise be 100% cross platform, I must maintain approx. 0,01% of platform specific code.