views:

38

answers:

2

I have taken (with the authors permission on the site) a wrapper class for writing fetch requests to my Core Data stack.

The guy who wrote it says it has "optional parameters" and I wondered what this meant.

Anywho, the static methods are written as such...

+(NSMutableArray *) searchObjectsFromContext
    : (NSString*) entityName
    : (NSPredicate*) predicate
    : (NSString*) sortKey
    : (BOOL) sortAscending
    : (NSManagedObjectContext *) managedObjectContext

I have been running it and passing "nil" into the unneeded params. Is this the correct thing to do?

Also, is there a significance to the fact that there is no extra method text between the colons?

Thanks for any help

Oliver

A: 

Optional parameters are available in VB (and some other languages) but not C#. C# forces you to overload functions.

You can pass "default" values to the optional parameters if you like. As long as you know how the function will respond, I don't see how this will hurt anything.

EDIT: I just read your iphone tag.... sorry! I don't know how relevant my answer is to you in that regard

Matthew PK
Thanks,The functions inside the wrapper deal with the various variables depending on whether they have a value I just wasn't sure about the term "optional variables".I guess he just means that you can pass nil in instead of actual values.Thanks!
Fogmeister
Don't worry, I think it's the same answer regardless. I should prob have put the fact that it's for iPhone in the title/post.Thanks
Fogmeister
+2  A: 

It's impossible to say if passing 'nil' is the correct thing to do without reading the documentation or examining the source code. It's probably correct, though, as that is standard behavior in most classes.

Creating method parameters without associated text (as you see in your posted code) is perfectly legal... but it's also perfectly ugly and you lose all the benefits of Obj-C's verbosity. There's absolutely no reason to write code like that, it only serves to make your classes more difficult to use.

kubi
Cool! Thanks!I thought it might just be laziness but wasn't sure if there was a significance that I didn't know about.
Fogmeister
Could not agree with @kubi more. As you get deeper into Objective-C you will find methods like that down right offensive. Also, that method really does not gain you a terrible amount. And finally, the fact that it returns a `NSMutableArray` from a Core Data fetch is just ... ***very very*** wrong. What site did this come from? I would love to look at the original source.
Marcus S. Zarra
I concur that whoever originally wrote this method has some seriously dangerous habits. I'd be leery of using code that I did not understand that came from that site.
TechZen
I'm not sure why NSMutableArray is a bad thing? That's what you get anyway from the "ExecuteFetchRequest" method. Also, the method has meant that for each request I can now do it all in 2 (or one very long) line of code rather than the 5 or 6 that were needed before.As I'm new to CoreData stuff could you please explain what else I could do with this?Thanks
Fogmeister
`executeFetchRequest` gives you an `NSArray`. In this case a mutable array doesn't make any sense, since your results are your results, you can't change them without going through a managed object context. The mutable array and the un-named parameters make me leery of this guy's code. If he's doing those two things wrong, there's no telling what other weird things he's doing.
kubi