nsnumber

NSString to NSNumber ignores initial zeros

泪湿孤枕 提交于 2019-12-14 03:44:33
问题 I am converting NSString to NSNumber . But the problem is that if NSString starts with zeros then converting it to NSNumber ignores the initial zeros. I know initial zeros doesn't mean anything but I want to keep initial zeros because it's phonenumber in my case. here is my code: NSLog(@"test1: %@",tempPhoneNumber); NSNumberFormatter *formatter=[[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *tempPnoneNumberNumber=[formatter

What's wrong with my code? It says unresolved Identifier

a 夏天 提交于 2019-12-13 03:29:37
问题 It says "use of unresolved identifier" for both NSNumber and NSNumber . I can't find a solution for this. I should be able to create a program that will convert each number from a given integer to its equivalent word value. The accepted value should be UInt16 only public extension Int { public var asWord: String { let numberValue = NSNumber(value: self) let formatter = NSNumberFormatter() formatter.numberStyle = .spellOut return formatter.string(from: numberValue)! } } var value = 2500 if

NSNumber, what to be aware of when using and storing different primitive types

本小妞迷上赌 提交于 2019-12-13 00:16:20
问题 An NSNumber can store different primitive types, like short, int, long, long long, float, double But does the size change, when i do @(long long) in comparison to @(int) By modeling the CoreData model, i use Integer16, Integer32, Integer64, but does it have an outcome on the size of the database, as all is NSNumber? To a CoreData property, which has in model defined Integer16 long long tmp = 83324; NSNumber * numberStoringLongLong = @(tmp); cdEntity.propertyInteger16 = numberStoringLongLong;

Check if NSNumber is fraction

心已入冬 提交于 2019-12-12 10:53:37
问题 What is the best way of checking if a NSNumber is a fraction? NumberIsFraction(@(0)); // NO; NumberIsFraction(@(0.5)); // YES; NumberIsFraction(@(1.0)); // NO; "Best" in terms of border case handling and performance. 回答1: Avoiding conversions to types with a smaller domain: BOOL NumberIsFraction(NSNumber *number) { double dValue = [number doubleValue]; if (dValue < 0.0) return (dValue != ceil(dValue)); else return (dValue != floor(dValue)); } 回答2: The solutions from Jonathan, hpique, and

NSExpression\NSNumber extension changed in Swift 3.0

我与影子孤独终老i 提交于 2019-12-12 03:34:23
问题 have some extension for NSNumber: extension NSNumber { func factorialF() -> NSNumber { return tgamma(self.doubleValue + 1) } } then i use this extension in my calculation var stringFunction: String = "FUNCTION(10,'factorialF')" var expn: NSExpression = NSExpression(format: stringFunction) var result = expn.expressionValueWithObject(with: nil, context: nil) in Xcode 7.3.1 and Swift 2.2 all works but in Xcode 8 and Swift 3.0 i have some error in my extension NSNumber "No 'tgamma' candidates

Convert NSNumber to long

北城余情 提交于 2019-12-11 16:27:50
问题 I am trying to convert a NSNumber to long but I get this error: [__NSSingleObjectArrayI intValue]: unrecognized selector sent to instance Here is my code: NSNumber *dbversion = [settings valueForKey:@"Version"]; long dbver = [dbversion longValue]; What am I doing wrong here? * settings is a NSArray and "Version" is the key for a long value. 回答1: You are caught in the Key-Value Coding trap. In some cases the result of valueForKey is an array which the error message clearly states. Don't Never

Trouble with NSDecimalNumber's decimalNumberByDividingBy:withBehavior:

若如初见. 提交于 2019-12-11 12:26:43
问题 I always seem to run into trouble with NSDecimalNumber! Today, I get this error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber decimalNumberByDividingBy:withBehavior:]: unrecognized selector sent to instance 0xd1fb10'" Here is the source of the error: - (void)setUpInstance { static NSDecimalNumberHandler* roundingBehavior = nil; if (roundingBehavior == nil) { roundingBehavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode

Get a char from NSString and convert to int

╄→尐↘猪︶ㄣ 提交于 2019-12-11 07:45:23
问题 In C# I can convert any char from my string to integer in the following manner intS="123123"; int i = 3; Convert.ToInt32( intS[i].ToString()); What is the shortest equivalent of this code in Objective-C ? The shortest one line code I've seen is [NSNumber numberWithChar:[intS characterAtIndex:(i)]] 回答1: Many interesting proposals, here. This is what I believe yields the implementation closest to your original snippet: NSString *string = @"123123"; NSUInteger i = 3; NSString

NSMutableArray : unrecognized selector sent to instance

青春壹個敷衍的年華 提交于 2019-12-11 02:53:04
问题 I'm trying to store an array int[9][9] with a NSMutableArray of NSMutableArray where I store my 81 integers from the array : - (void)awakeFromNib { // initialization matrix for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { matrix[i][j] = 0; } } // Creating NSMutableArray instance TGrid = [NSMutableArray arrayWithCapacity:10]; [self saveGrid]; } - (void)saveGrid { NSNumber *aInt; NSMutableArray *Grid = [NSMutableArray arrayWithCapacity:81]; for (int i = 0; i < 9; i++) { for (int j =

Two NSNumbers created from the same float aren't equal according to ==

对着背影说爱祢 提交于 2019-12-10 15:33:58
问题 I have a few float s which I convert to NSNumber s and add to an array, then sort in numerical order. I then take the original float s and convert those to NSNumber s, but if I try to compare them if(numinarray = newnum){ it never goes anywhere. When I NSLog() out the value, the values match: float distance = [barnardCastle distanceFromLocation:loc1] / 1000; float distance2 = [billingham distanceFromLocation:loc1] / 1000; float distance3 = [bishopAukland distanceFromLocation:loc1] / 1000;