nsnumber

Sorting Number using NSSortDescriptor

扶醉桌前 提交于 2019-12-07 05:28:23
问题 I got stuck in another problem again but after a long time. This time I have database (Core Data), having an attribute of numbers which contains integer numbers like 213879,123,4,345,56567 and so. I need to fetch data in ascending number order similar to like alphabetically order. I am doing this in way given below, fetchRequest.sortDescriptors=[NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"numbers" ascending:YES selector:@selector(compare:)]]; but unfortunately it

How to cast from UInt16 to NSNumber

半腔热情 提交于 2019-12-06 20:34:50
问题 I have a UInt16 variable that I would like to pass to a legacy function that requires an NSNumber. If I try: var castAsNSNumber : NSNumber = myUInt16 I get a compiler error 'UInt16' is not convertible to 'NSNumber' Question How can I recast this as an NSNumber? 回答1: var castAsNSNumber = NSNumber(unsignedShort: myUInt16) 来源: https://stackoverflow.com/questions/24134716/how-to-cast-from-uint16-to-nsnumber

Pick closest NSNumber from array

纵然是瞬间 提交于 2019-12-06 16:02:57
问题 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],

String to float in objective c

穿精又带淫゛_ 提交于 2019-12-06 03:04:17
问题 I have a parser returning some string value I'd like to use as parameter for my class instance initialisation. I have a method asking two NSString and a float value, but I can't convert the string to float, here is my code: NSString *from = [[NSString alloc] initWithString:@"EUR"]; NSString *to = [[NSString alloc] initWithString:[attributeDict objectForKey:@"currency"]]; NSNumber *rate = [[NSNumber alloc] initWithFloat:[[attributeDict objectForKey:@"rate"] doubleValue]]; currency = [[Currency

NSNumber from CGFloat

旧街凉风 提交于 2019-12-05 17:49:48
问题 Is there a safe way to "convert" a CGFloat to a NSNumber ? NSNumber has the numberWithFloat: and numberWithDouble: methods but CGFloat being defined as float or double depending on the platform, it seems risky to use either one of them. Or is numberWithDouble: safe to use with a CGFloat , as it is the one with the more precision ? 回答1: I believe @ NSNumber literal is good enough @(myCGFloat) Read more here: http://clang.llvm.org/docs/ObjectiveCLiterals.html 回答2: This is how I handled it:

NSNumber double with many decimal places being rounded/truncated

泄露秘密 提交于 2019-12-05 12:37:08
I have a double in an NSNumber . double myDouble = 1363395572.6129999; NSNumber *doubleNumber = @(myDouble); // using [NSNumber numberWithDouble:myDouble] leads to the same result This is where it gets problematic. doubleNumber.doubleValue seems to return the correct and full value (1363395572.6129999) However, looking at doubleNumber in the debugger or doing doubleNumber.description gives me (1363395572.613) . I would understand if perhaps this was just some display formatting, but when I then stick this object into a JSON payload, the messed up rounded value gets inserted instead of the

Sorting Number using NSSortDescriptor

◇◆丶佛笑我妖孽 提交于 2019-12-05 10:58:41
I got stuck in another problem again but after a long time. This time I have database (Core Data), having an attribute of numbers which contains integer numbers like 213879,123,4,345,56567 and so. I need to fetch data in ascending number order similar to like alphabetically order. I am doing this in way given below, fetchRequest.sortDescriptors=[NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"numbers" ascending:YES selector:@selector(compare:)]]; but unfortunately it compares only the 1st digit of every number, mean if there are 2 numbers like 123 and 321 , it will compare 1

Converting (u)int64_t to NSNumbers

喜你入骨 提交于 2019-12-05 02:13:37
So essentially my question is this, I am creating an NSMutableDictionary using uint64_t objects as the key. Is there any better way to create them than doing this? uint64_t bob=7; NSNumber *bobsNumber; #if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 bobsNumber=[NSNumber numberWithUnsignedLong:bob]; #else bobsNumber=[NSNumber numberWithUnsignedLongLong:bob]; #endif This would work as long as you didn't include it in a binary file/sockets/NSData object/whatever. But is there any better way of doing this? I really would like to be sure that the

How to cast from UInt16 to NSNumber

旧时模样 提交于 2019-12-05 01:35:46
I have a UInt16 variable that I would like to pass to a legacy function that requires an NSNumber. If I try: var castAsNSNumber : NSNumber = myUInt16 I get a compiler error 'UInt16' is not convertible to 'NSNumber' Question How can I recast this as an NSNumber? var castAsNSNumber = NSNumber(unsignedShort: myUInt16) 来源: https://stackoverflow.com/questions/24134716/how-to-cast-from-uint16-to-nsnumber

iOS - Why Does It Work When I Compare Two NSNumbers With “==”?

筅森魡賤 提交于 2019-12-05 01:14:49
In my app, I accidentally used "==" when comparing two NSNumber objects like so: NSNumber *number1; NSNumber *number2; Later on, after these objects' int values were set, I accidentally did this: if (number1 == number2) { NSLog(@"THEY'RE EQUAL"); } And, confusingly, it worked! I could have sworn I was taught to do it this way: if (number1.intValue == number2.intValue) { NSLog(@"THEY'RE EQUAL"); } How did using "==" between the two NSNumber objects work, and why? Does that mean it's okay to compare them that way, or was it just a fluke and this is generally not guaranteed to work every time? It