Daniele B
02/15/2023, 5:48 PMvar path : MutableList<ScreenIdentifier> = mutableListOf()
retrieving Binding Swift Array => successful! 🎉
extension Binding where Value == NSMutableArray {
public func cast() -> Binding<[ScreenIdentifier]> {
return Binding<[ScreenIdentifier]>(get:{ self.wrappedValue as NSArray as! [ScreenIdentifier] },
set: { self.wrappedValue = NSMutableArray(array: $0) })
}
}
===
NavigationStack(path: $path.cast()) {
...
}
===
=== Kotlin MutableMap of MutableList to binding Swift Array ===
Kotlin MutableMap of MutableLists definition:
var paths : MutableMap<String, MutableList<ScreenIdentifier>> = mutableMapOf(),
retrieving Binding Swift Array => not working 😩
extension Binding where Value == NSMutableDictionary {
public func getPath(level1URI: String) -> Binding<[ScreenIdentifier]> {
return Binding<[ScreenIdentifier]>(
get:{
let dict = self.wrappedValue as NSDictionary as! [String:[ScreenIdentifier]]
return dict[level1URI] as! NSMutableArray as NSArray as! [ScreenIdentifier]
},
set: {
var modifiedDict = self.wrappedValue as! [NSString:NSMutableArray]
modifiedDict[level1URI as NSString] = NSMutableArray(array: $0)
self.wrappedValue = NSMutableDictionary(dictionary: modifiedDict as! KotlinMutableDictionary<NSString,NSMutableArray>)
}
)
}
}
===
NavigationStack(path: $paths.getPath(level1URI: myString)) {
...
}
===
this is the error given by XCode:
Referencing instance method 'getPath(level1URI:)' on 'Binding' requires the types 'KotlinMutableDictionary<NSString, NSMutableArray>' and 'NSMutableDictionary' be equivalent
Daniele B
02/15/2023, 9:38 PMMutableMap
cannot cast directly to NSMutableDictionary
, unlike MutableList
which can instead cast directly to NSMutableArray
.
So, the solution is to ignore NSMutableDictionary
and to deal directly with KotlinMutableDictionary
.
Here is the correct extension code:
extension Binding where Value == KotlinMutableDictionary<NSString,NSMutableArray> {
public func getPath(level1URI: String) -> Binding<[ScreenIdentifier]> {
return Binding<[ScreenIdentifier]>(
get:{
let dict = self.wrappedValue as! [String:[ScreenIdentifier]]
return dict[level1URI]!
},
set: {
var modifiedDict = self.wrappedValue as! [NSString:NSMutableArray]
modifiedDict[level1URI as NSString] = NSMutableArray(array: $0)
self.wrappedValue = KotlinMutableDictionary<NSString, NSMutableArray>.init(dictionary: modifiedDict)
}
)
}
}