iwatakeshi
iwatakeshi

Reputation: 697

Is it possible to do arithmetic with methods in Objective-C?

In C# I can add getters anIntVariable = someMethod1() + someMethod2()... but in Objective-C, I a get a warning, "Implicit Declaration of function 'someMethod1'is invalid in c99. What is the equivalent way to do arithmetic with methods? Thanks.

Upvotes: 1

Views: 88

Answers (2)

Dave
Dave

Reputation: 7717

This is pretty basic stuff; recommend you take a moment to read more about Obj-C syntax if you're interested in picking up the language.

This is the equiv of your example.

NSInteger anIntVariable = [self someMethod1] + [self someMethod2];

If "someMethod1" is the getter for a "property", you may also be able to write "self.method1" to get the value of that property.

Objective-C is a fun language. I think you'll love it if you give it a chance.

Good luck.

Upvotes: 1

Firo
Firo

Reputation: 15566

You can do this in Objective C.

NSInteger myNumber = [self method1] + [self method2]; // myNumber is 3

Where method1 & method2 might be implemented like:

- (NSInteger)method1 {
     return 1;
}

- (NSInteger)method2 {
     return 2;
}

Upvotes: 1

Related Questions