nsnumber

Pick closest NSNumber from array

只谈情不闲聊 提交于 2019-12-04 20:02:53
I have an array with a bunch of NSNumber s. From an UISlider I get a certain value when the user stops dragging it. I would like to get the closes number from the array. So for instance, if the user drags the UISlider to 13 , and the NSArray contains the NSNumbers with 10 and 15 ; I want to get 15 from the array. Example of array: NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:15], [NSNumber numberWithInt:20], [NSNumber numberWithInt:30], [NSNumber numberWithInt:45], [NSNumber numberWithInt:60], [NSNumber numberWithInt:90], [NSNumber numberWithInt:110], nil]; How do I get

Objective c: Check if integer/int/number

拜拜、爱过 提交于 2019-12-04 17:42:11
问题 In objective c, how can i check if a string/NSNumber is an integer or int 回答1: You can use the intValue method on NSString: NSString *myString = @"123"; [myString intValue]; // returns (int)123 Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue Hope that helps! 回答2: If you're trying to

Comparing NSNumber to 0 not working?

 ̄綄美尐妖づ 提交于 2019-12-04 15:44:50
问题 I have a JSON parser in my app, and I load the value into a detailDataSourceDict variable. When I try to get the valueForKey of the array and try to compare it to 0, it never works... Here's my code: if (indexPath.row == 1) { NSNumber *rating = [detailDataSourceDict valueForKey:@"rating"]; NSLog(@"Rating: %@",rating); if (rating == 0) { cell.detailTextLabel.text = @"This sheet has not yet been rated."; } else { cell.detailTextLabel.text = [NSString stringWithFormat:@"This sheet has a %@ star

NSNumber compare: returning different results

我怕爱的太早我们不能终老 提交于 2019-12-04 04:42:06
I'm trying to do some number comparisons and I'm getting some weird results. NSNumber* number1 = [NSNumber numberWithFloat:1.004]; NSNumber* number2 = [NSNumber numberWithDouble:1.004]; ([number1 compare:number2] == NSOrderedSame) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 compare:number2] == NSOrderedAscending) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 doubleValue] == [number2 doubleValue]) ? NSLog(@"YES") : NSLog(@"NO"); ([number1 floatValue] == [number2 floatValue]) ? NSLog(@"YES") : NSLog(@"NO"); Log output: NO YES NO YES This is extremely frustrating to me. I know this is probably because

How do you get the int and modulo (mod) of division with an NSDecimalNumber

我怕爱的太早我们不能终老 提交于 2019-12-04 04:30:43
I am confused by NSDecimalNumber and its "behaviors". I have an NSDecimalNumber that represents a dollar value, say $37.50. I'd like to find out how many times say 5.0 goes into that number and then know what's left over. I can get the straight division and get 7.50 but I want 7 mod 2.50. I could convert to an integer but need to save the "cents" so wondering if there's some tricks in the framework? adam Using Peter Hoseys example, but with iOS code: NSDecimalNumber *dividend = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:37.5] decimalValue]]; NSDecimalNumber *divisor

Does NSNumberFormatter.stringFromNumber ever return nil?

淺唱寂寞╮ 提交于 2019-12-04 03:09:02
问题 It seems to me that any valid number can also be expressed as a String , so I don't know why this function returns a String? instead of a String . 回答1: My best guess would be because of the legacy support. This is from the official documentation: The behavior of an NSNumberFormatter object can conform either to the range of behaviors existing prior to OS X v10.4 or to the range of behavior since that release. NSNumberFormatter Class Reference 来源: https://stackoverflow.com/questions/28103113

check if NSNumber is empty

做~自己de王妃 提交于 2019-12-03 15:18:11
问题 How do I check if a NSNumber object is nil or empty? OK nil is easy: NSNumber *myNumber; if (myNumber == nil) doSomething But if the object has been created, but there is no value in it because an assignment failed, how can I check this? Use something like this? if ([myNumber intValue]==0) doSomething Is there a general method for testing objects on emptiness like for NSString available (see this post)? Example 1 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setValue:@

Objective c: Check if integer/int/number

[亡魂溺海] 提交于 2019-12-03 10:31:24
In objective c, how can i check if a string/NSNumber is an integer or int You can use the intValue method on NSString: NSString *myString = @"123"; [myString intValue]; // returns (int)123 Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue Hope that helps! Anshu Chimala If you're trying to determine whether or not an NSString has a numeric value or not, try using NSNumberFormatter . -(BOOL)

Comparing NSNumber to 0 not working?

Deadly 提交于 2019-12-03 09:46:58
I have a JSON parser in my app, and I load the value into a detailDataSourceDict variable. When I try to get the valueForKey of the array and try to compare it to 0, it never works... Here's my code: if (indexPath.row == 1) { NSNumber *rating = [detailDataSourceDict valueForKey:@"rating"]; NSLog(@"Rating: %@",rating); if (rating == 0) { cell.detailTextLabel.text = @"This sheet has not yet been rated."; } else { cell.detailTextLabel.text = [NSString stringWithFormat:@"This sheet has a %@ star rating.",rating]; } cell.textLabel.text = @"Rating"; } I see in my JSON feed that "rating":"0", but

How get the total sum of NSNumber's from a NSArray?

泄露秘密 提交于 2019-12-03 09:36:21
I have a large NSArray containing NSNumbers like 3, 4, 20, 10, 1, 100, etc... How do I get the total sum of all these NSNumbers (3 + 4 + 20 + 10 + 1 + 100 + etc...) as one total NSInteger ? Thank you! NSInteger sum = 0; for (NSNumber *num in myArray) { sum += [num intValue]; } You can use this: NSArray* numbers = //array of numbers NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"]; fabrice truillot de chambrier long long sum = ((NSNumber*)[array valueForKeyPath: @"@sum.longLongValue"]).longLongValue; Iterate through the array int count = [array count]; NSInteger sum = 0; for (int i = 0;