ObservableCollection: how to use the FirstOrDefault? - observablecollection

I have a custom class, with a property ID. We can name this class A, and the property ID.
I have an observable collection of A, and I would like to get the firstOrDefault to know if an object exists with a determined ID. Soy I do the following:
myObersableCollection.FirstOrDefault(a=>a.ID==2)
But I get the following error: it is not possible to convert implicitly A into bool.
What am I doing wrong?
Thanks.
Daimroc.

FirstOrDefault() returns a matching object, not a boolean.
If you just want to check whether there is a matching object, call .Any() instead.

Related

Why do Property class's decorator methods in Python return a copy of the Property object and not the object itself?

The documentation provided on python.org for class Property, states that:
"A property object has getter, setter, and deleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function."
What I can't wrap my head around is this: why make a new copy (and hence the overhead of duplication) of the existing Property object, with an extra accessor function set in place, and not just simply augment it (the object) by filling in that accessor function into its corresponding vacant slot (fset / fdel), and then return it, the original object itself?

NSCoder - Only Called On Object instantiation

I have set up a class that conforms to NSCoding, and works as expected when I first create the object. It is essentially the same set up as this
The problem I am having is that the properties of the object when changed are not kept.
For example,
Foo is created and has a property called name.
Foo.name = #"bar"
I can encode / decode the object and it retains the name bar.
If I try and change
Foo.name = #"newName"
The encode method is not called again, so foo.name remains as #"bar"
(I have a log state within the encode method)
Also,
I am using a core data object, that has a transformable property which points to the foo object.
Thanks
To "save" the object, you have to call the encode method, e.g. to write it to disk or send it to an output stream.
However, since you are using Core Data to persist the object, you have to call
[managedObjectContext save:&error];
to persist the object after changing it.
That being said, I do not think it makes a lot of sense to have a transformable property that points to a custom class that keeps a string property. Instead, you should think of a more appropriate data structure so you only need transformable properties for non standard data types that cannot be persisted by using the standard data types already built into Core Data.

Use NSExpression CAST with NSPredicate in Core Data

My problem is exactly the same as you can read in this thread:
stackoverflow thread
But there is a small difference. I try to explain it using the above thread's image.
The CS relationship contains C type objects as it is described, but also contains E type objects too, because the E type objects are derived from the C type object.
On thread's image the E type object is not present and this is the difference. The E type property also has a CS relationship.
The problem comes when I try to reach the E type object's CS property using the following query: SUBQUERY(bs, $x, ANY $x.cs.cs ....
The query is not finished, but the important code is there. As I observed, the predicate creation fails, because it tries to use the CS relationship on the C type object. I've tried to use the CAST operator on the $x.cs variable without success.
Do anybody has a clue for this?
Edit: Added image
The values of the MKMultiAttribute entity can contain MKAttribute and MKMultiAttribute types. And this is the problem, because the values is set to be a relationship to MKAttribute, but, because MKMultiAttribute is derived from MKAttribute the values can contain MKMultiAttribute entities and currently I can't call the values again.

Referencing an instance of a given class in sequence diagrams

I have to model a system where an object of the class Person will invoke the static method getBook(...) : Book on the class Book which will return an instance of a particular book.
How do you reference the book instance obtained by the operation?
As of now, I can think of two approaches, neither of which I have ever seen/used, which is why I am looking for the correct approach.
The first approach is to invoke methods directly on the book instance obtained, e.g. if the reference returned by getBook(...) : Book is named matchingBook, I would use matchingBook.doSomething(...), much like having a local variable.
The second way, which I find more in the line of sequence diagrams is to let the book instance returned by the operation appear with its own lifeline, e.g. next to the Book class, and referencing it with an arrow labeled doSomething(...).
However, with the second approach, it is not that obvious that this object is in fact the one returned by the operation.
The second approach is the correct. To show that you are pointing to the returned object (matchingBook), you can add the variable name to the title of the lifeline, like this:
The second approach is the correct one. Anytime you call operations on an object returned by a first operation, you can't do better than a name match between the result of the first call and the lifeline.
Anyway I don't really understand what you expect of the first way: where would you put matchingBook.doSomething(...)? on a arrow pointing which lifeline?

IDispatch object that responds to all properties?

I want to create an IDispatch object that returns a value for every property. Ask it for "foo", it returns something. "bar" returns something. "faid1jhgi31jifj" as well.
Any pointers?
You need to override the GetIDsForNames methods with an appropriate implementation that returns a valid DISPID for any input parameters. Then override the Invoke method to ensure to return the correct value based on the dispatch id.
You should also look at IDispatchEx, which is designed to give more flexibility for dynamic interfaces.

Resources