Satisfying how to handle nsnumber objects in an arithmetic operation

I saw this topic, but wanted to confirm:

How to convert NSNumber objects for computational purposes?

So basically, anytime you want to deal with these objects, do you need to unpack your ivars and then pack them back into new objects, presumably NSNumbers?

It looks like hella weakness (and a lot of pain in the back, no?).

How do you work with them?

Do you avoid them? Subclass them? are there any mutable versions?

It just seems like a lot of work to deal with them, I would like to hear their advantages and how they use more experienced programmers or what tactics they used to avoid using them.

Thank,

Nick

+3
3

, , , ivars, , NSNumbers?

. (, doubleValue ivar. , .)

( , ?).

"" , ref-counting Objective-C (Foundation.framework). , NSNumber, NSArray.

?

.

?

, , :

  • , NSNumber, NSValue. , , objCType NSValue, - . "c", "C", "s", "S", "i", "I", "l", "L", "q", "Q", "f" "".

, . -numberByAddingNumber:, :

@implementation NSNumber (MyExtension)
-(NSNumber*)numberByAddingNumber:(NSNumber*)another {
   double myVal = [self doubleValue];
   double anotherVal = [another doubleValue];
   return [NSNumber numberWithDouble:myVal + anotherVal];
}
@end
...
NSNumber* a, *b;
...
NSNumber* c = [a numberByAddingNumber:b];
...

?

.

+6

NSNumbers, . -, , Core Data - .

+3

Now that there are Objective-C literals in the newest version of the clang compiler (version 3.2 up, comes with Xcode 4.6, and can also be built from source code), you can do things like @ 42 and @ (7 + 35) to "box" NSNumbers.

+2
source

Source: https://habr.com/ru/post/1728208/


All Articles