0

I've just started learning Objective-C and I'm not sure when to use dot notation vs. square brackets. I know a similar question has been asked here but I'm still not really understanding the difference. I've read that [myObject doSomething] and myObject.doSomething are equivalent. What I'm not sure about though is that when I use @property to generate a method it automatically generates a setter method that I can only use with square brackets.

// MyClass.h

@interface MyClass : NSObject
@property int number; 


// main.m
MyClass *object = [[MyClass alloc] init];
[object setNumber:3];
object.setNumber = 4; // Property 'setNumber' not found

Why am I getting error messages when I write object.setNumber = 4 and why can I only use square brackets for this?

jscs
  • 63,694
  • 13
  • 151
  • 195
Striving
  • 35
  • 8
  • 1
    use `object.number = 4` – Bryan Chen Apr 14 '14 at 04:07
  • 1
    The equivalent statement with dot notation is object.number = 4, not object.setNumber = 4. Dot notation uses the name of the property itself, not the name of the setter. – rdelmar Apr 14 '14 at 04:07
  • 2
    You can use the dot notation whenever you have a method that takes 0 or 1 parameters. The problem with your code is (why you get the warning) that you must *not* use `set` in dot notation. Why is that? The compiler generates a normal method call from dot notation. So if you write `object.number = 4;` the compiler generates `[object setNumber:4];`. That's why it doesn't work when you write `object.setNumber = 4;` because the compiler generates `[object setSetNumber:4];` and that method doesn't exist. – HAS Apr 14 '14 at 04:10

2 Answers2

0

You get that error because the property is called number not setNumber. It is common in OOP languages to create methods that access an object's data using "setters" and "getters." A property in Objective-C 2.0 synthesizes this process and automatically generates the setters and getters. If you go into MyClass.m and enter - setN you will see setNumber suggested by Xcode. That is because you can implement your setters and getters yourself to customize their behavior, or leave the default behavior of simply setting a value or getting a value.

Additionally, properties automatically generate the variable declaration as _variableName For instance, the default behavior of your property would be:

- (void) setNumber:(int)number {
    _number = number
}

- (int) getNumber {
    return _number;
}

Determining whether you should use one over the other really comes down to consistency. To ensure consistency in teams, a Style Guide should be in place. Take the NY Times for instance. They restrict dot notation strictly to Properties and brackets for non-property methods (i.e. setters and getters).

Chris
  • 1,663
  • 1
  • 15
  • 19
-1

You're confusing methods and properties.

When you make a property, yes it creates a setter method called setVARIABLE.

The only way to call a method is with the bracket notation. IE [object setNumber:3]; (Unless you have 0, or 1 parameters, thanks HAS)

What you want to do is use the property name, not the setter (or getter) method.

[object number];

That will return the value of number. You can't read properties when accessing them through bracket notation, it's only readonly (only accessing the get function).

object.number = 3;

That's how you would set the property number using dot notation.

When you wrote object.setNumber = 3; you were treating setNumber like a property, which it's not, it's a method.

To recap

  1. methods are only usable by bracket notation, not dot notation [object setNumber:3]; (Unless you have 0, or 1 parameters)
  2. you can only read a property when accessing it through bracket notation [object number]; //returns the value of number
  3. you can read and write a property when using it through dot notation object.number = 3;
Michael King
  • 640
  • 1
  • 6
  • 18
  • 1
    Your "variable" terminology is off. `[object number]` is a message send just like `[object doThatThingYouDo]`. It's not "reading a variable"; a method is being run; its return value may be the value of an instance variable, but that's not inherent. Neither is `object.number` direct access of a variable. It's syntactic sugar for, again, (a message send that results in) a method call. "The only way to call a method is with the bracket notation. IE `[object setNumber:3];`" is thus not correct. – jscs Apr 14 '14 at 04:46
  • Sorry for lack of correct terminology. I updated my answer to include correct terminology. – Michael King Apr 14 '14 at 04:56