setEditable 和 isEditable

发布于 2024-12-10 03:44:11 字数 424 浏览 1 评论 0原文

您好,我是 Objective C 的新手,当我阅读开发人员文档时,我经常看到这一点。

有名为 setEditable 或 setWhateverName 和 isEditable 或 isWhateverName 的方法

set"blabla" 和 is"blabla" 之间有什么区别

,示例是 NSButton 类

-setAlternateTitle

-alternateTitle

NSImageView 类

-isEditable

-setEditable:

我认为这两个示例中的后者都需要参数,而前者则不需要。这是正确的吗?

Hi I'm new to objective c and I see this quite often when I read the developer documentation.

There are methods named setEditable or setWhateverName and isEditable or isWhateverName

What is the difference between the set"blabla" and is"blabla"

and example would be for the NSButton Class

-setAlternateTitle

and -alternateTitle

or

NSImageView class

-isEditable

and -setEditable:

I think that the latter in both examples takes a paramater and the former ones do not. Is this correct?

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

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

发布评论

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

评论(3

谈场末日恋爱 2024-12-17 03:44:11

这些称为变异方法。它们是您用于访问各种Objective-C 属性,基本上是类成员变量(所谓的ivars实例变量),除非您无法直接访问它们。您只能通过 settergetter 方法访问 ivars。

getter 方法获取属性的当前值。对于非布尔属性,它通常被赋予与相关属性相同的名称(例如 -alternateTitle),或者带有 is 前缀的名称(例如 -isEditable) 用于布尔属性。

setter 方法为属性设置一个新值。它通常被赋予以 set 为前缀的属性名称,例如 -setEditable:

例如:

if ([myObject isEditable])  // Is the object editable?
    /* do stuff */ ;
...
[myObject setEditable:YES];  // Make it editable

These are called mutator methods. They are methods you use for accessing various Objective-C properties, which are basically class member variables (so-called ivars or instance variables), except you can't access them directly. You can only access the ivars through the setter and getter methods.

The getter method gets the current value of the property. It's usually given a name identical to the property in question (such as -alternateTitle) for non-boolean properties, or the name with an is prefix (such as -isEditable) for boolean properties.

The setter method sets a new value for the property. It's usually given a name of the property name prefixed with set, e.g. -setEditable:.

For example:

if ([myObject isEditable])  // Is the object editable?
    /* do stuff */ ;
...
[myObject setEditable:YES];  // Make it editable
美羊羊 2024-12-17 03:44:11

-setAlternateTitle-alternateTitle 是基于标准命名的默认 setter 和 getter(例如 NSObjectint)惯例。

setEditableisEditable 是布尔 (BOOL) 属性的传统 setter 和 getter。在布尔情况下,它提高了可读性。请注意,这里的变化仅存在于 getter 中。

-setAlternateTitle and -alternateTitle are the default setter and getter (e.g. NSObject or int), based on standard naming conventions.

setEditable and isEditable is the conventional setter and getter for boolean (BOOL) properties. In the boolean case, it improves readability. Note that the variation here is only in the getter.

月下凄凉 2024-12-17 03:44:11

定居者和吸气者有一个模式。

对于非 BOOL ivars,

  • setter 有一个“set”前缀:“setMyIvar”——注意大写
    “M”。
  • getter 只是 ivar 名称:“myIvar”——注意没有“get”前缀。 *

这些是属性创建的默认值,例如:

@property (nonatomic, retain) (NSString *)myString;

对于 BOOL ivars 有两种模式:

  • setter 有一个“set”前缀:“setMyBoolIvar”——注意大写的“M”。
  • getter 只是 ivar 名称:“myBoolIvar”——注意没有“get”前缀。 *

  • setter 有一个“is”前缀:“isMyBoolIvar”——注意没有“get”前缀和大写“M”。

这些是由属性创建的默认值,例如:

@property (nonatomic, assign) (BOOL *)myBoolIvar;

或 分别

@property (nonatomic, assign, getter=isMyBoolIvar) (BOOL *)myBoolIvar;

There is a pattern to settlers and getters.

For non BOOL ivars

  • the setter has a "set" prefix: "setMyIvar" -- note the upper case
    "M".
  • the getter is simply the ivar name: "myIvar" -- notice there is no "get" prefix. *

These are the defaults created by the property, ex:

@property (nonatomic, retain) (NSString *)myString;

For BOOL ivars there are two patterns:

  • the setter has a "set" prefix: "setMyBoolIvar" -- note the upper case "M".
  • the getter is simply the ivar name: "myBoolIvar" -- notice there is no "get" prefix. *

    or

  • the setter has a "is" prefix: "isMyBoolIvar" -- notice there is no "get" prefix and the upper case "M".

These are the defaults created by the property, ex:

@property (nonatomic, assign) (BOOL *)myBoolIvar;

or respectively

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