开发者

setEditable and isEditable

开发者 https://www.devze.com 2023-04-13 07:55 出处:网络
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

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

an开发者_Go百科d -setEditable:

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


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


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


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;
0

精彩评论

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