Questions tagged [nsnumber]

on Mac OS X, NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.)

Resource

785 questions
581
votes
18 answers

How to convert an NSString into an NSNumber

How can I convert a NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contain at runtime. I have an idea how to do it, but I'm not sure…
Enyra
  • 17,542
  • 12
  • 35
  • 44
137
votes
7 answers

How to convert NSNumber to NSString

So I have an NSArray "myArray" with NSNumbers and NSStrings. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details =…
dav3
  • 1,447
  • 2
  • 10
  • 9
132
votes
5 answers

Convert NSNumber to int in Objective-C

I use [NSNumber numberWithInt:42] or @(42) to convert an int to NSNumber before adding it to an NSDictionary: int intValue = 42; NSNumber *numberValue = [NSNumber numberWithInt:intValue]; NSDictionary *dict = @{ @"integer" : numberValue }; When I…
suse
  • 10,503
  • 23
  • 79
  • 113
86
votes
6 answers

How to add two NSNumber objects?

Now this must be easy, but how can sum two NSNumber? Is like: [one floatValue] + [two floatValue] or exist a better way?
mamcx
  • 15,916
  • 26
  • 101
  • 189
73
votes
9 answers

String to NSNumber in Swift

I found a method to convert String to NSNumber, but the code is in Objective-C. I have tried converting it to Swift but it is not working. The code I am using: NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; f.numberStyle =…
user2413621
  • 2,916
  • 7
  • 24
  • 28
69
votes
5 answers

Comparing NSNumbers in Objective C

I am a beginner at Objective-C and I am a bit confused at this scenario. I have the following code: if (number1 < number2) { NSLog(@"THE FOLLOWING NUMBER "); NSLog(@"%@", number1); NSLog(@"IS LESS THAN"); NSLog(@"%@",…
Doug Molineux
  • 12,283
  • 25
  • 92
  • 144
66
votes
26 answers

iOS convert large numbers to smaller format

How can I convert all numbers that are more than 3 digits down to a 4 digit or less number? This is exactly what I mean: 10345 = 10.3k 10012 = 10k 123546 = 123.5k 4384324 = 4.3m Rounding is not entirely important, but an added plus. I have looked…
Kyle Begeman
  • 7,169
  • 9
  • 40
  • 58
62
votes
5 answers

What's the difference between NSNumber and NSInteger?

What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?
mk12
  • 25,873
  • 32
  • 98
  • 137
53
votes
9 answers

Determine if NSNumber is NaN

How can I determine if a Cocoa NSNumber represents NaN (not a number)? This emerges, for example, when I parse a string that has an invalid (non-numeric) contents.
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
45
votes
3 answers

Is it possible to cast an NSInteger to NSNumber?

Is it possible to cast a NSInteger to a NSNumber object? I need to convert the tag of a UIImageView object to a NSNumber object because I need to pass it as an argument to a function.
Fitzy
  • 1,871
  • 6
  • 23
  • 40
45
votes
5 answers

Working with NSNumber & Integer values in Swift 3

I am trying to convert my project to Swift 3.0 however I am having two error messages when working with NSNumber and Integers. Cannot assign type int to type NSNumber for //item is a NSManaged object with a property called index of type NSNumber…
A.Roe
  • 973
  • 3
  • 15
  • 34
43
votes
2 answers

Convert NSNumber to NSDecimalNumber

I realize that NSDecimalNumber inherits from NSNumber. However, I am struggling to figure out how I can init an NSDecimalNumber from an NSNumber or how to convert an NSNumber to an NSDecimalNumber. I have tried NSDecimalNumber* d = [aNumber…
Michael Rivers
  • 920
  • 2
  • 10
  • 17
42
votes
12 answers

get type of NSNumber

I want to get the type of NSNumber instance. I found out on http://www.cocoadev.com/index.pl?NSNumber this: NSNumber *myNum = [[NSNumber alloc] initWithBool:TRUE]; if ([[myNum className] isEqualToString:@"NSCFNumber"]) { // process NSNumber as…
okami
  • 2,093
  • 7
  • 28
  • 40
40
votes
7 answers

Objective-C: Find numbers in string

I have a string that contains words as well as a number. How can I extract that number from the string? NSString *str = @"This is my string. #1234"; I would like to be able to strip out 1234 as an int. The string will have different numbers and…
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
34
votes
5 answers

Convert CGFloat to NSNumber in Swift

I have an extension in Swift that holds some properties which are CGFloat's. The problem is that I don't know how to get the store the CGFloat value as a NSNumber using the associated objects Here is the code I have that doesn't work but it details…
FireDragonMule
  • 1,347
  • 2
  • 16
  • 27
1
2 3
52 53