开发者

Is there any way to give a format to arithmetic computations in Objective-C?

i want to define something similar to a computation method:

NSString *format = @"%d + 1";

In my code i want to do something like:

int computedNumber = su开发者_开发问答m(format,5) => the result should be 6.

could you give some suggestions? thank you

EDIT: or something similar to: NSNumber *no = [NSNumber numberWithFormat:format,5];


It is not normally possible, however there have been written parsers for this specific tasks, to mention DDMathParser by SO user Dave DeLong.

But for what task do you really need this? You have the + operator, and then you perform the sum function? Couldn't you simply parse the number at the end of the format, then perform the operation you'd like?


Using Macro can be an alternative solution to your problem. You can define a macro like the following,

#define INC(a) (a + 1)

Here, INC and a are user-defined. You can give them any name you want. Compiler will substitute (a + 1) in your code, where ever you call INC(a). For example, consider the following,

int computedNumber = INC(5);

After the compilation the code will be,

int computedNumber = (5 + 1); // the result during the execution is 6.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜