When to avoid using accessor methods in Objective C

Jimmery

I keep seeing things like this in example Objective C code:

_myProp = newValue;

As far as I understand, creating a property myProp will create the actual variable as _myProp, and two accessor methods which are (by default) myProp (the getter) and setMyProp (the setter).

My question is, why do I keep seeing example code that by-passes the accessor methods and gets at the variable directly? Is there any advantage to this?

Nikolai Ruhe

There are occasions where you don't want to declare a property and instead just use a private ivar.

If, on the other hand, there is a property declaration there should be no reason to directly access the ivar, except in the implementation of the accessors.

Sometimes people want to bypass the side effects of accessors and use the ivar directly. This is usually a sign of an architectural flaw, though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When to avoid using accessor methods in Objective C

From Dev

Checking protocol conformance when using factory methods in Objective-C

From Dev

Using Swift clases with overloaded methods in Objective C

From Dev

Using delegate methods with semaphores in Objective C

From Dev

How to avoid using "and" in Objective-C method name

From Dev

How to avoid code duplication when using controllers with similar methods?

From Dev

How to avoid code duplication when using controllers with similar methods?

From Dev

order of methods in objective C

From Dev

Objective-C: Using inheritance and calling methods in Child Class

From Dev

Objective-C: using methods from other files

From Dev

Benefits when using static init in Objective C?

From Dev

Objective C - Avoid Import Loop

From Dev

How to make class accessor to private in Framework(Objective-C)

From Dev

Are declared property attributes overridden when implementing custom accessor methods?

From Dev

Why create accessor methods when I can just use KVC?

From Dev

Getting around having to @synthesize when changing accessor methods in subclasses

From Dev

When are accessor methods for S4 objects called in R?

From Dev

Why create accessor methods when I can just use KVC?

From Dev

How to avoid using static methods

From Dev

Using Accessor methods to access data from another class

From Dev

Factory methods in Objective-C

From Dev

Avoid using global variables when using recursive functions in C

From Dev

Class and accessor methods Java

From Dev

Dereferencing a pointer when using NSLog in Objective-C

From Dev

"Cannot Find Initializer" when using Objective-C class in Swift

From Dev

Address is different when using pointer with __block in Objective-C

From Dev

BAD_ACCESS when using a weak reference in Objective C

From Dev

which methods are called when the phone is put to sleep versus minimized and then put to sleep in objective-c

From Dev

How do we implement custom init methods in objective-c when we use storyboard?

Related Related

  1. 1

    When to avoid using accessor methods in Objective C

  2. 2

    Checking protocol conformance when using factory methods in Objective-C

  3. 3

    Using Swift clases with overloaded methods in Objective C

  4. 4

    Using delegate methods with semaphores in Objective C

  5. 5

    How to avoid using "and" in Objective-C method name

  6. 6

    How to avoid code duplication when using controllers with similar methods?

  7. 7

    How to avoid code duplication when using controllers with similar methods?

  8. 8

    order of methods in objective C

  9. 9

    Objective-C: Using inheritance and calling methods in Child Class

  10. 10

    Objective-C: using methods from other files

  11. 11

    Benefits when using static init in Objective C?

  12. 12

    Objective C - Avoid Import Loop

  13. 13

    How to make class accessor to private in Framework(Objective-C)

  14. 14

    Are declared property attributes overridden when implementing custom accessor methods?

  15. 15

    Why create accessor methods when I can just use KVC?

  16. 16

    Getting around having to @synthesize when changing accessor methods in subclasses

  17. 17

    When are accessor methods for S4 objects called in R?

  18. 18

    Why create accessor methods when I can just use KVC?

  19. 19

    How to avoid using static methods

  20. 20

    Using Accessor methods to access data from another class

  21. 21

    Factory methods in Objective-C

  22. 22

    Avoid using global variables when using recursive functions in C

  23. 23

    Class and accessor methods Java

  24. 24

    Dereferencing a pointer when using NSLog in Objective-C

  25. 25

    "Cannot Find Initializer" when using Objective-C class in Swift

  26. 26

    Address is different when using pointer with __block in Objective-C

  27. 27

    BAD_ACCESS when using a weak reference in Objective C

  28. 28

    which methods are called when the phone is put to sleep versus minimized and then put to sleep in objective-c

  29. 29

    How do we implement custom init methods in objective-c when we use storyboard?

HotTag

Archive