Iterating over an NSOrderedSet
You can iterate over an ordered set with
let orderedSet = NSOrderedSet(array: [ 42, 43, 44])
orderedSet.enumerateObjectsUsingBlock { (elem, idx, stop) -> Void in
println("\(idx): \(elem)")
}
UPDATE: As of Swift 1.2 (Xcode 6.3), NSOrderedSet
conforms toSequenceType
and can be enumerated with for ... in ...
:
let orderedSet = NSOrderedSet(array: [ 42, 43, 44])
for elem in orderedSet {
println(elem)
}
NSOrderedSet and SwiftUI ForEach
The following should help you
ForEach(Array(currentDog.walks!.set),id: \.self) { walk in
Text("Date \(self.walkDate(walk))")
}
How to ForEach in SwiftUI a NSOrderedSet from FetchedResults (CoreData)
The highlighted error is because there is no view specified in ForEach
, so if to define something static, like
ForEach(Array(card.withChilds!.set), id: \.self) { child in
Text("Just test")
}
there should be no error (tested with Xcode 11.4)
But there is no specified what is inside that NSOrderedSet
, so if suppose it is instances of Child
class then the following tested works
ForEach(Array(card.withChilds!.set), id: \.self) { child in
Text("someText: \((child as! Child).text1)")
}
Iterate NSSet and cast to type in one step
You can use pattern matching case let
in a for
loop:
for case let animal as Animal in zoo {
if animal.needFeeding {
}
}
Note that this will just skip elements which are not instances of
(a subclass of) Animal
.
Iterate over a NSSet - SwiftUI 2.0
Try the following (compiled with Xcode 12.1 / iOS 14.1)
ForEach(Array(team.people as? Set<Player> ?? []), id: \.self){ player in
Text("\(player.name ?? "")" )
}
Caution: try to avoid force unwrapping for optionals, very often it will result in run-time crash.
Iterate through NSManagedObject in order
A to-many relationship is represented as an NSSet
when using valueForKey:
.
To turn that into a sorted NSArray
, you can use the sortedArrayUsingDescriptors:
method:
NSSet *allFields = [table valueForKey:@"fields"];
NSSortDescriptor *indexSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES];
NSArray *sortedFields = [allFields sortedArrayUsingDescriptors:@[indexSortDescriptor]];
Find object in NSOrderedSet in CoreData. index(ofAccessibilityElement :)
Autocompletion can and often is wrong. It can be handy but it's not a reference for what's correct.
I don't know what you mean about the compiler wanting to use that method, because it's not what you need to look up objects in an ordered set. The correct method would be index(of:)
, as in
var myset = NSMutableOrderedSet(array: [1, 2, 3])
myset.index(of:2)
Related Topics
Cllocation Distancefromlocation (In Swift)
Curl with Alamofire - Swift - Multipart/Form-Data
How to Create an Uppercase Version of a String in Swiftui
Formsheet iOS 8 Constraints Are Same as Iphones Constraints
Using @Fetchrequest(Entity: ) for Swiftui MACos App Crashes
Nsposixerrordomain When Binding to Socket on MACos 10.12
Xctestcase Optional Instance Variable
Swiftui Hierarchical Picker with Dynamic Data
Generate Avaudiopcmbuffer with Avaudiorecorder
A Codable Structure Contains a Protocol Property
Setting Arkit Orientation via Swift
Iterating Over an Nsorderedset
Swiftui Sheet Not Animating Dismissal on MACos Big Sur
Closures Return Value (Previously Completionblock)
How to I Access Argv and Argc in Swift
How to Implement a Swift Protocol Across Structs with Conflicting Property Names