What is the definition of Convenience Method in regards to Objective C?
Asked Answered
B

2

9

In most languages I have dealt with, one something is called a convenience method, it means that the method does some small task that gets done very frequently, and therefore it is more convenient to use said method.

In Objective-C does this definition hold true? Or is it generally only used to describe class methods that return a prebuilt object? ex. [NSString stringWithContentsOfFile:...]

Is this just a preference thing, or are there some hard and fast definition for these terms?

Cheers, Stefan

Bacchanal answered 23/2, 2011 at 20:42 Comment(0)
P
10

What you are talking about is actually more specifically a "convenience constructor" in Objective C. (Note that it's not really a constructor in the C++/Java/C# sense, it's actually an object initializer/factory method, but it seems to be the convention to call the "convenience constructors"). "Convenience constructors" in Obj C are a convention or pattern for creating a constructor/initializer/factory method for a class which takes specific parameters. This pattern also has some special conventions that you should follow (such as autoreleasing the new object within the constructor), so that your custom classes fit in well with the built-in types.

See this page (a little way down) for more info: http://macdevcenter.com/pub/a/mac/2001/07/27/cocoa.html?page=3

As for "convenience method," this specific term doesn't have any special meaning in Objective C. You can create any type of convenience method in Obj C, and there is no expectation about what it should or should not do. It's only "convenience constructor" that has a special meaning.

Promptbook answered 23/2, 2011 at 20:47 Comment(4)
Not "constructor," but "factory" or "initializer" (though the latter is usually reserved for actual init methods only.) Objective-C does not have constructors.Hazan
Good point... it does seem to be the convention to call these "Convenience constructors" thoughPromptbook
In a lot of ways i think of the convenience methods that return prebuilt objects as factory methods.Bacchanal
@Jonathan Grynspan: As Andy says, Objective-C does call these methods "constructors." They're similar to Java factory methods, but in general the term "constructor" is favored for such methods in Objective-C, and "factory" is not used very much. Apple's documentation uses both terms and states that they are equivalent. I think the distinction might be that singleton accessors are also considered factory methods, so "convenience constructor" is viewed as more specific.Haycock
T
5

So far as I know, "convenience method" means basically what you defined it to mean here: a single method or function which replaces a more complicated series of invocations because of its frequency of use.

In Objective-C, the "ordinary" way to create a new instance is something along the lines of NSSomething * mySomething = [[[NSSomething alloc] initWithParam:... andParam:...] autorelease]. Many classes provide convenience constructors which simplify these three steps (in fact, in most cases, they probably literally do exactly the same thing, but wrapped behind a class method call).

Tauromachy answered 23/2, 2011 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.