0

Im reading a book and in the sample code they show how to assign the value of a NSTextField to another NSTextField like this:

self.theStory.text = self.theTemplate.text;

When I try to do the same without using Dot Notation:

 [[self theStory]text] = [[self theTemplate]text];

I get the following error: Expression is not assignable.

Im new to Objective-C, any help will be appreciated.

3 Answers3

0

Something like this:

[[self theStory] setText:[[self theTemplate]text]]
TheNextman
  • 12,428
  • 2
  • 36
  • 75
0

When you use brackets instead of dots, you need to use the full name of the setter method, and drop the =.

[[self theStory] setText:[[self theTemplate] text]];

Dot notation, in my experience, is best used for setting or getting state, by writing or reading a property or keypath (a keypath is a chain of methods, for instance self.datamodel.members.count). Bracket notation is a better choice when you're performing some sort of action. There are cases where it's possible to use dot notation for methods without parameters, but that makes for confusing code. For instance, if I had an NSTimer instance myTimer, I would use [myTimer fire], not myTimer.fire.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
0

That's because you can't do that. Dot notation has two equivalents, the "name" form and the "setName:" form. You'd use [[self theStory] setText:[[self theTemplate] text]];.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • My background is in C# and it makes perfect cense to me to use Dot notation however; many of my Objective C friends prefer Bracket notation and they have advised me get used to using bracket notation. What do you recommend? and by the way thank you for the quick reply. – Walter Torres Mar 29 '13 at 19:02
  • There are arguments for both. First off, in theory dot notation is ONLY valid for properties, and not all "name"/"setName:" pairs implement properties. And in some cases using brackets makes things clearer (hard as that is to believe). Plus ARC has muddled the whole issue in several ways. – Hot Licks Mar 29 '13 at 19:50
  • Not quite right. Dot notation is perfectly valid for setters and getters, even if they're declared manually instead of with `@property`. And it's valid, although confusing, for zero-parameter methods that are not getters. – Hal Mueller Mar 29 '13 at 21:38
  • Depends on which translation of the gospel you adhere to. – Hot Licks Mar 29 '13 at 22:06