M. Ryan
M. Ryan

Reputation: 7192

Is there a better way to write this NSNumber operation?

There has to be.

Two objects, each with an NSNumber property. I want to add the value of one into the other.

I came up with

int count = [[cat resultCount] intValue];
count += [[allTours resultCount] intValue];
[allTours setResultCount:[NSNumber numberWithInt:count]];

Which is ridiculous. But I wasn't sure I could just randomly add NSNumber objects together given how temperamental they are with logical comparisons.

Anyone got a better way? (Not using NSNumber, unfortunately, is not an option. I would if I could)

edit

This is technically a duplicate of How to add two NSNumber objects? but I don't know how to flag it as such or whatever...

Upvotes: 1

Views: 347

Answers (1)

Evan Mulawski
Evan Mulawski

Reputation: 55334

You can combine it into one line, but there is nothing you can do about NSNumber - it is a class, and you can't perform math operations on classes:

[allTours setResultCount:[NSNumber numberWithInt:([[cat resultCount]intValue] + [[allTours resultCount]intValue])]];

Upvotes: 2

Related Questions