1

I have a Java program I am converting into objective-c.

In Java I uses dot notation and for for example have the following line of code:

this.m_root.computeGlobalTransforms();

Which calls a method called computeGlobalTransforms().

Am I able to use dot notation like this in objective-c or will I need to write the line as and pass 'm_root' as an object:

[self computeGlobalTransforms:m_root];
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
samb90
  • 1,063
  • 4
  • 17
  • 35

3 Answers3

4

If computeGlobalTransforms is an instance method of the class m_root object, and if m_root is a class property of self, then the syntax is:

[self.m_root computeGlobalTransforms]. 

I'd suggest you refer to Methods and Messaging in Learning Objective-C: A Primer.


If computeGlobalTransforms is a VA_Bone method, the syntax would be:

VA_Bone *bone = [[VA_Bone alloc] init];
[bone computeGlobalTransforms];

Your bone variable is a simple local variable, thus no reference to self.m_root is needed.


By the way, as a matter of convention, the underscores in the middle of variable and class names is generally not used. See Naming Properties and Data Types in the Coding Guidelines for Cocoa. Also see references to "camel case" in the Conventions of Programming with Objective-C.

Thus, a more common naming convention would be VABone for your class. Likewise for your original m_root property that you alluded to, you'd (a) give it a more descriptive name; and (b) use camel case. Now I don't know what the "m" of m_root is supposed to stand for (which, alone, illustrates the problem), but let's say it it was for medium sized images, then it might be mediumSizedImagesRoot, or whatever.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I'm having trouble with the following code: VA_Bone *bone = [[VA_Bone alloc] init]; [self.m_root [bone computeGlobalTransforms]]; I want to call the method computeGlobalTransforms which is found in a class called Bone on the self.m_root object. This does not appear to work, what have I done wrong? – samb90 Feb 26 '13 at 18:17
  • @samb90 See my revised answer. – Rob Feb 26 '13 at 19:42
3

You can use dot notation to call zero parameter methods. You absolutely shouldn't. Dot notation is handled by the compiler, rather than the run time - there's a very good answer to a similar question that talks about why it works, and why you shouldn't use it (...for anything other than setters/getters) here:

Objective-C dot notation with class methods?

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
0

No you can't, you can only use Dot.Notation for setters and getters for your properties.

Simon
  • 25,468
  • 44
  • 152
  • 266
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
  • 3
    That's not the case - it's terrible style to use it for anything other than properties though: http://stackoverflow.com/a/2375991/197143 – lxt Feb 26 '13 at 16:30