Objective C 中便利方法的定义是什么?

发布于 2024-10-19 01:56:28 字数 229 浏览 7 评论 0原文

在我接触过的大多数语言中,有一种方法被称为便利方法,这意味着该方法执行一些经常完成的小任务,因此使用该方法更方便

在 Objective-C 中这个定义成立吗?或者它通常只用于描述返回预构建对象的类方法?前任。 [NSString stringWithContentsOfFile:...]

这只是一个偏好,还是这些术语有一些硬性和快速的定义?

干杯, 斯特凡

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-10-26 01:56:28

您所说的实际上是 Objective C 中更具体的“便捷构造函数”。(请注意,它并不是真正的 C++/Java/C# 意义上的构造函数,它实际上是一个对象初始值设定项/工厂方法,但它似乎是调用“便利构造函数”的约定)。 Obj C 中的“便捷构造函数”是一种约定或模式,用于为采用特定参数的类创建构造函数/初始化程序/工厂方法。此模式还具有一些您应该遵循的特殊约定(例如在构造函数中自动释放新对象),以便您的自定义类能够很好地适应内置类型。

请参阅此页面(稍往下)了解更多信息:http://macdevcenter.com/pub/a/mac/2001/07/27/cocoa.html?page=3

至于“便捷方法”,这个特定术语没有任何特殊含义您可以在 Obj C 中创建任何类型的便捷方法,并且不期望它应该做什么或不应该做什么。只有“便利构造函数”才有特殊的含义。

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.

独夜无伴 2024-10-26 01:56:28

据我所知,“便捷方法”基本上意味着您在这里定义它的含义:由于其使用频率,单个方法或函数取代了一系列更复杂的调用。

在 Objective-C 中,创建新实例的“普通”方式类似于 NSSomething * mySomething = [[[NSSomething alloc] initWithParam:... andParam:...] autorelease]。许多类提供了简化这三个步骤的便捷构造函数(事实上,在大多数情况下,它们可能实际上执行完全相同的操作,但包装在类方法调用后面)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文