dMurdZ
dMurdZ

Reputation: 1079

Defining calculated constants in objective c

I'm trying to make it so that when a user creates my UIView subclass, MyView, he can pass an integer, and then some calculations are done on this integer and a constant variable is set to the integer's new calculated value. What's the best practice for setting something like this up?

I know #define exists (although I don't know how'd you do calculations) and I know there is a const keyword, but I've never used these. I've accomplished stuff like this in the past with properties but I know that's not correct.

Upvotes: 0

Views: 284

Answers (2)

Sebastian Wramba
Sebastian Wramba

Reputation: 10127

#define is simply a redefinition in your code. Does not do anything useful. If I understand you correctly, you might want to define a custom constructor for your UIView subclass which accepts the desired parameter.

- (instancetype)initWithParam:(NSInteger)someInteger;

And since you are doing a calculation based on the input parameter, it is not actually a constant, so you just might want to write a method for this.

- (NSInteger)someCalculatedProperty;

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

You can simply define it like this:

integer_t const x= 10;

Also you may try like this:

#define x 10

although I don't know how'd you do calculations

I am not sure what you mean by calculation but if you are trying to do some calculation in the constant itself then avoid that. As then it would not be a constant.

Upvotes: 1

Related Questions