Comments (6)
OrderedDictionary can be used with ForEach
, List
etc.. via several ways.
ForEach (0..<orderedDictionary.count) { index in dictionary.elementAt(index)}
ForEach ([CustomObject] (orderedDictionary.orderedValues)) { value in }
(using orderedKeys is the same)
Note that conforming Identifiable
and proper implementation is important, otherwise rendered views may be duplicated or wrongly ordered.
from ordereddictionary.
I've experimented with using your OrderedDictionary (and indeed Dictionary) with ForEach, and decided the results that are specific to working with SwiftUI called for a different approach, and did that. I do have an array but had to make a slightly different arrangement to accommodate the ForEach.
I would for sure recommend holding off on any changes until after WWDC; I expect Apple to offer one, perhaps two minor adjustments to the SwiftUI pantheon that could affect development outcomes, like the existing SwiftUI code in my project. Thanks!
from ordereddictionary.
@crenelle: I’ve looked into this issue and although I most likely will be able to make OrderedDictionary
conform to RandomAccessCollection
in the upcoming version, it won’t be of any help with its use in SwiftUI’s ForEach
. The reason is that ForEach
requires the elements of Data
to conform to Identifiable
. Since the elements of OrderedDictionary
are tuples holding both the key and value, it’s not possible to make them conform to any protocol and thus they cannot be used directly. However, there are other ways, but it highly depends on what you’re trying to achieve.
The simplest way is to base it on indices as follows:
ForEach(orderedDictionary.indices) { index in
let (key, value) = orderedDictionary[index]
// Use index, key, value
}
If you want SwiftUI to keep track of elements’ identities, you can make the keys conform to Identifiable
and leverage the orderedKeys
property while loading values for them as follows:
ForEach(orderedDictionary.orderedKeys) { key in
let value = orderedDictionary[key]
// Use key, value
}
I hope this helps. If you have any follow-up questions, let me know.
Edit: You might need a conversion to Array
in the second case which would then read like this ForEach(Array(orderedDictionary.orderedKeys))
. In version 4.0, I’m currently working on, this won’t be necessary anymore.
from ordereddictionary.
Hi Michael, there hasn’t been any special work done for this. I’m not sure how ForEach
exactly works since I’m not using SwiftUI myself. There is a slight chance that it could work out of the box since OrderedDictionary
is an iterable collection. You can give it a try and see for yourself. Creating a test project and adding this library via SPM should quickly answer your question.
from ordereddictionary.
The error message I get that seems to get right to the point is:
Generic struct 'ForEach' requires that 'OrderedDictionary<String, Any>' conform to 'RandomAccessCollection'
from ordereddictionary.
Thanks for the tests. OrderedDictionary
only conforms to BidirectionalCollection
and not RandomAccessCollection
.
I’ve looked into why this is and I couldn’t find any reason not to conform to RandomAccessCollection
. In fact, when doing my quick tests, it worked out of the box when changing the conformance from BidirectionalCollection
to RandomAccessCollection
. The only change necessary would be a straightforward implementation of formIndex(after:)
and formIndex(before:)
methods. However, I’d need to do more testing on this before publishing such change and since the WWDC is around the corner, I won’t have time for this soon.
As a temporary workaround, you should be able to convert your OrderedDictionary
instance to an array of key-value pairs using Array(orderedDictionary)
and iterate over that in the list. I hope this helps.
from ordereddictionary.
Related Issues (20)
- Integrate containment optimizations from maicki's fork
- Add a note to README about no official support for CocoaPods
- Key based access fails with error when Dictionary key is an integer type HOT 6
- Package.swift seems invalid for Swift 4.2.1 HOT 2
- Fix warnings for Swift 5.0
- OrderedDictionary does not serialize to JSON using JSONSerialization or JSONEncoder HOT 1
- how to use cocoapods? HOT 1
- how to convert [String:[[String:AnyObject]]] to OrderedDictionary type? HOT 1
- V3.0.0 CFBundleShortVersionString missing HOT 3
- Move LinuxMain.swift to /Tests
- Cannot insert duplicate key HOT 2
- `filter` should return a new OrderedDictionary, not an array of Key-Value pairs HOT 2
- Direct access to keys array instead of the lazy variant only
- Precondition Failed: Inconsistency error occurred in OrderedDictionary HOT 2
- Apple m1 support
- Inspect memory leaks in implementation on v4.0 branch
- Xcode 13 Build Issues HOT 2
- Convert to a proper SPM package and remove Xcode project
- The Server SSL certificate failed to verify HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ordereddictionary.