nsnumber

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

扶醉桌前 提交于 2019-11-27 14:45:37
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:true).dynamicType.className() == "__NSCFBoolean" // true NSNumber(bool:true).isEqualToValue(true) ||

iOS convert large numbers to smaller format

青春壹個敷衍的年華 提交于 2019-11-27 10:39:29
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! Luca Iaco -(NSString*) suffixNumber:(NSNumber*)number { if (!number) return @""; long long num = [number longLongValue]; int s = ( (num < 0) ? -1 : (num > 0) ? 1 : 0 ); NSString* sign = (s == -1 ?

How to determine the true data type of an NSNumber?

让人想犯罪 __ 提交于 2019-11-27 09:32:38
Consider this code: NSNumber* interchangeId = dict[@"interchangeMarkerLogId"]; long long llValue = [interchangeId longLongValue]; double dValue = [interchangeId doubleValue]; NSNumber* doubleId = [NSNumber numberWithDouble:dValue]; long long llDouble = [doubleId longLongValue]; if (llValue > 1000000) { NSLog(@"Have Marker iD = %@, interchangeId = %@, long long value = %lld, doubleNumber = %@, doubleAsLL = %lld, CType = %s, longlong = %s", self.iD, interchangeId, llValue, doubleId, llDouble, [interchangeId objCType], @encode(long long)); } The results: Have Marker iD = (null), interchangeId =

Determine if NSNumber is NaN

人走茶凉 提交于 2019-11-27 07:57:36
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. So, I found out that the class property [NSDecimalNumber notANumber] is just for this purpose. In some languages NaN != NaN, but this isn't the case in Cocoa. As Mike Abdullah says, the natural way to represent a NaN in Cocoa is with nil , but [NSNumber numberWithDouble:NAN] does return a valid object. There is no NSNumber -specific way of detecting this, but the general way, isnan([foo doubleValue]) , works. If you don’t like

Storing and retrieving unsigned long long value to/from NSString

偶尔善良 提交于 2019-11-27 06:55:06
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 value, not the unsigned long long value). Can someone please tell me how I can get back the value into an

setProgress is no longer updating UIProgressView since iOS 5

99封情书 提交于 2019-11-27 06:48:23
问题 I have a little trouble with a progress bar since iOS 5 came out. The code below was working fine before iOS 5 but with iOS 5 the progress bar is no longer displaying the new progress that is set within a loop. The code is expected to work like this: Create the progress bar (works) In a new background process: Set an initial progress of 0.25 (works) In the same background process: Update the progress while going thru the loop (worked in iOS 4) Here's the code for the bar init: // create a

Why NSNumber points to the same address when value are equals?

拜拜、爱过 提交于 2019-11-27 05:53:02
问题 Given the following code: int firstInt, secondInt; firstInt = 5; secondInt = 5; NSNumber *firstNumber = [NSNumber numberWithInt:firstInt]; NSNumber *secondNumber = [NSNumber numberWithInt:secondInt]; Why on Earth do those two NSNumber instances are pointing to the same address? This drives me crazy! Of course, if you change secondInt to, say '4', all works as expected. Thanks, Jérémy 回答1: This is likely either a compiler optimisation or an implementation detail: as NSNumber is immutable there

NSNumber of seconds to Hours, minutes, seconds

守給你的承諾、 提交于 2019-11-27 04:39:34
问题 I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is there a method that spits that out? I can't seem to find a proper formatter. 回答1: Have you tried creating an NSDate from that and printing that using an NSDateFormatter ? NSDate *date = [NSDate dateWithTimeIntervalSince1970:[theNumber doubleValue]]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter

What's the difference between NSNumber and NSInteger?

拟墨画扇 提交于 2019-11-27 04:10:28
问题 What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats? 回答1: The existing answers are useful; adding to them: Yes, NSUInteger gives twice the range among positive integers as NSInteger , but I think another critical reason to choose between the two is simply to distinguish among cases where negative values simply do not make sense . Example: the return value of NSArray 's count method is an NSUInteger ,

Comparing NSNumbers in Objective C

落花浮王杯 提交于 2019-11-27 04:09:21
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 helloworld[1014:207] 96.75866 Both numbers are NSNumber objects, how could something like this happen? I am