Getting a String Value from Nsorderedset Using Swiftui Foreach

Getting a String value from NSOrderedSet using SwiftUI ForEach

You can use array method on NSOrderedSet and force typecast it to [SessionNote], since you know that you will always be storing SessionNote class to the ordered set.

ForEach(selectedClient.clientNotes!.array as! [SessionNote], id: \.self) { (note: SessionNote) in
Text(note.text ?? "")
}

Since this casting will be necessary in every places you wish to use the notes. You could also add a computed property inside your Client which always gives you typed version of the SessionNote array.

extension Client {
var typedClientNotes: [SessionNote] {
return (clientNotes?.array as? [SessionNote]) ?? []
}
}

And, with these new changes you can make your ForEach bit better.

ForEach(selectedClient.typedClientNotes, id: \.self) { note in
Text(note.text ?? "")
}

For this reason, I try to use normal Set for Core data relationship. Because it can be typed and makes working with Coredata so much fun. But, NSOrderedSet has its own advantages despite of being an untyped collection.

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 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.

Resolving optional one-to-one relationship for use with SwiftUI ForEach?

You would deal with it here

MyView(aItem: item, bItem: item.B ?? BManager.create(itemA: item))

BManager being a Manager object that would hold a convenience method to create a blank object.

That way if B happens to be nil an object would get created immediately.

Here is something similar it involves a struct<->CoreData relationship so it isn't exact but similar to this.

SwiftUI ForEach, conditionals and showing one result of many

you could try something like this approach, without using the ForEach loop (because they require the entries to be unique):

struct ForEachLoopTesting: View {

let start = ["a", "b", "c", "a"]
let this = ["a", "b"]

var body: some View {
if startContains(this) {
Text("yes start contains this")
} else {
Text("no start does not contain this")
}
}

func startContains(_ this: [String]) -> Bool {
var result = false
outerLoop: for test1 in start {
for test2 in start {
if ([test1, test2] == this) {
result = true
break outerLoop
}
}
}
return result
}

// alternative
func startContains2(_ this: [String]) -> Bool {
for test in this {
if !start.contains(test) {
return false
}
}
return true
}

}

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)

Cannot convert value of type 'NSSet?' to expected argument type 'RangeInt' (using CoreData)

ForEach does not understand NSSet, you have to convert it in array:

if subject.topics != nil {
ForEach(Array(subject.topics! as Set), id: \.self) { topic in
NavigationLink(
destination: Text("Destination"),
label: {
Text("Navigate")
})
}
}


Related Topics



Leave a reply



Submit