nsnumber

How to add two NSNumber objects?

邮差的信 提交于 2019-11-27 03:22:28
Now this must be easy, but how can sum two NSNumber ? Is like: [one floatValue] + [two floatValue] or exist a better way? Louis Gerbarg There is not really a better way, but you really should not be doing this if you can avoid it. NSNumber exists as a wrapper to scalar numbers so you can store them in collections and pass them polymorphically with other NSObjects . They are not really used to store numbers in actual math. If you do math on them it is much slower than performing the operation on just the scalars, which is probably why there are no convenience methods for it. For example:

How to convert NSNumber to NSString

微笑、不失礼 提交于 2019-11-27 02:59:43
So I have an NSArray "myArray" with NSNumber s and NSString s. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil]; details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"]; The subjectText works. But how can I get the NSNumber s out of it? (I actually need them as strings...) I would convert a NSString out of a NSNumber like this: NSString *blah = [NSNumber intValue] . But

Is there a correct way to determine that an NSNumber is derived from a Bool using Swift?

自古美人都是妖i 提交于 2019-11-26 16:53:45
问题 An NSNumber containing a Bool is easily confused with other types that can be wrapped in the NSNumber class: NSNumber(bool:true).boolValue // true NSNumber(integer: 1).boolValue // true NSNumber(integer: 1) as? Bool // true NSNumber(bool:true) as? Int // 1 NSNumber(bool:true).isEqualToNumber(1) // true NSNumber(integer: 1).isEqualToNumber(true) // true However, information about its original type is retained, as we can see here: NSNumber(bool:true).objCType.memory == 99 // true NSNumber(bool

iOS convert large numbers to smaller format

眉间皱痕 提交于 2019-11-26 15:16:11
问题 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 into NSNumberFormatter but have not found the proper solution, and I have yet to find a proper solution here on SO. Any help is greatly appreciated, thanks! 回答1: -(NSString*) suffixNumber:(NSNumber*)number { if (!number) return @""; long long num =

Comparing NSNumbers in Objective C

帅比萌擦擦* 提交于 2019-11-26 12:43:18
问题 I am a beginner at Obj-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(@\"%@\", number2); } When I run this code I see really strange results like this: 2011-07-06 20:38:18.044 helloworld[1014:207] THE FOLLOWING NUMBER 2011-07-06 20:38:18.047 helloworld[1014:207] 190.8776 2011-07-06 20:38:18.050 helloworld[1014:207] IS LESS THAN 2011-07-06 20:38:18.053

How to convert NSNumber to NSString

爱⌒轻易说出口 提交于 2019-11-26 12:37:16
问题 So I have an NSArray \"myArray\" with NSNumber s and NSString s. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details = [[DetailViewController alloc] initWithNibName:@\"DetailView\" bundle:nil]; details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@\"subject\"]; The subjectText works. But how can I get the NSNumber s out of it? (I actually need them as

Storing and retrieving unsigned long long value to/from NSString

感情迁移 提交于 2019-11-26 12:14:13
问题 I have an unsigned long long value which I want to store into an NSString and retrieve from the string. Initially I have the value in an NSNumber and I am using this to get the string NSString *numStr = [NSString stringWithFormat:@\"%llu\", [myNum unsignedLongLongValue]]; where myNum is an NSNumber. To get back the NSNumber from the NSString I have to first get the unsigned long long value. But there is no method in the NSString class to do that (we just have one for getting the long long

get type of NSNumber

爷,独闯天下 提交于 2019-11-26 09:28:31
问题 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 integer } else if ([[myNum className] isEqualToString:@\"NSCFBoolean\"]) { // process NSNumber as boolean } Ok, but this doesn\'t work, the [myNum className] isn\'t recognized by the compiler. I\'m compiling for iPhone. 回答1: I recommend using the -

Is it possible to replicate Swifts automatic numeric value bridging to Foundation (NSNumber) for (U)Int8/16/32/64 types?

自闭症网瘾萝莉.ら 提交于 2019-11-26 08:26:42
问题 Question Is it possible to replicate Swifts numeric value bridging to Foundation:s NSNumber reference type, for e.g. Int32 , UInt32 , Int64 and UInt64 types? Specifically, replicating the automatic by-assignment bridging covered below. Intended example usage of such a solution: let foo : Int64 = 42 let bar : NSNumber = foo /* Currently, as expected, error: cannot convert value of type \'Int64\' to specified type \'NSNumber */ Background Some of the native Swift number (value) types can be

How to convert an NSString into an NSNumber

寵の児 提交于 2019-11-25 23:45:36
问题 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 if this works with any type, also unsigned and floating point values: long long scannedNumber; NSScanner *scanner = [NSScanner scannerWithString:aString]; [scanner scanLongLong:&scannedNumber]; NSNumber *number = [NSNumber numberWithLongLong