开发者

Most straightforward method of getting an NSArray to an NSManagedObject

开发者 https://www.devze.com 2023-02-27 10:47 出处:网络
This is a very basic question but I\'m struggling to find the answer. I have an NSManagedObject called iSelectedObject.

This is a very basic question but I'm struggling to find the answer. I have an NSManagedObject called iSelectedObject.

Supposing I have a condition later on that pulls either representedObject from an NSOutlineView or selectedObjects from an NSArrayController, depending on (irrelevant to this question) criteria, I then need to run a method that requires an NSManagedObject to be fed to it. This works fine when I feed it the representedObject from the NSOutlineView - it's naturally an NSManagedObject when picked out this way. However, if I grab the selectedObjects of my NSArrayController, it's an NSArray and I can't feed it to my method.

I'm aware that an NSArray can contain NSManagedObjects and feel the solution maybe as simple as going one level deeper into my NSArray or something similar but how would I feed my NSArray to my method that requires and NSManagedObject. Should I extract/convert it in some way?

(As a help: The reason that I was even trying to feed the method the NSArray is because it and the, alternative, NSManagedObject both have the same data held inside. ie, if I make two functions exactly the same with one expecting an NSArray and one expecting an NSManagedObject, they work perfectly. The only reason I have separate choices of picking out an NSArray or NSManagedObject is because that seems to开发者_高级运维 be the default that selectedObjects and representedObject spit out, respectively. If I could pull out one or the other for both, that would be the perfect solution but I'm unsure how to.)


If the NSArray only contains one object, you could just pull the NSManagedObject out of the NSArray with

[array lastObject];

If there are multiple objects, run through the array and test each object with

[object isKindOfClass:[NSManagedObject class]];

Either way, that will give you an NSManagedObject that you can use for your method.


I was able to change the method to expect a type id rather than an NSManagedObject and this stopped it complaining.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号