1

I'm new to Objective-C 2.0, but very familiar with C++.

In C++ I would do the following inside a classes .h or header file, but I can't seem to do this in Objective-C 2.0 in XCode 4.0.

Is there an 'Objective-C 2.0' way to do this?

Example

In HEADER FILE:

// Header File

class MyClass
{
    private:
            float _myFloat;
    public:
           (float) getMyFloat { return _myFloat; }
};

The idea being that I don't have to go into the .cpp file to add the 'getMyFloat' method, I can just do that inside the header.

When I try and do this in XCode 4.0 with Objective-C 2.0 it gives me errors.

-(float) getMyFloat { return _myFloat; } 

Thanks for any advice.

Cullen
  • 13
  • 2

1 Answers1

1

There is no way to do this in Objective-C, method implementations may only appear in the @implementation block.

While it might be a convenient bit of syntactic sugar, there wouldn't be any real advantage to doing so anyway. In C++ it allows inlining of the method, but that wouldn't work for Objective-C for the same reasons that inline virtual functions are not often useful in C++. Especially considering that you cannot have an actual object variable rather than a pointer in Objective-C, and that the actual implementation of any method (even dealloc) can be changed at runtime using class_replaceMethod, method_exchangeImplementations, or categories loaded from a bundle.

Community
  • 1
  • 1
Anomie
  • 92,546
  • 13
  • 126
  • 145