nsnumber

Conversion between CGFloat and NSNumber without unnecessary promotion to Double

吃可爱长大的小学妹 提交于 2019-12-10 13:54:29
问题 As we all know, CGFloat (which is ubiquitous in CoreGraphics, UIKit etc) can be a 32-bit or 64-bit floating point number, depending on the processor architecture. In C, CGFloat it is a typealias to float or double , in Swift is it defined as a struct CGFloat with a native property (which is Float or Double ). It has been observed repeatedly that a NSNumber can be created from and converted to Float and Double , but that there exist not similar conversions from and to CGFloat . The general

Converting (u)int64_t to NSNumbers

血红的双手。 提交于 2019-12-10 03:26:14
问题 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

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

雨燕双飞 提交于 2019-12-10 01:12:01
问题 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

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

杀马特。学长 韩版系。学妹 提交于 2019-12-09 07:41:34
问题 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! 回答1: NSInteger sum = 0; for (NSNumber *num in myArray) { sum += [num intValue]; } 回答2: You can use this: NSArray* numbers = //array of numbers NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"]; 回答3: long long sum = ((NSNumber*)[array valueForKeyPath: @"@sum.longLongValue"]).longLongValue;

Xcode debugging: View value of NSNumber?

落爺英雄遲暮 提交于 2019-12-09 01:52:31
问题 Is it possible to see the numeric value of an NSNumber in the debugger datatip on in the variable watch window? I store an Integer value in NSNumber and want to see this value during debugging. I tried some of the data formatters in the debugger already, but it wasn't much help. 回答1: Open the Debugger view and in the Summary column enter {(int)[$VAR intValue]} or whatever interpretation is most appropriate for what you want to see in the Debugger. 来源: https://stackoverflow.com/questions

Storing optional NSNumber in Core Data

时间秒杀一切 提交于 2019-12-08 17:26:23
问题 In my core data model, I have an entity with an optional NSNumber attribute. How do I test to see if the value in that attribute is valid or not? When I test for nil... doesn't work. [self numberAttribute] == nil // always returns NO When I test for a zero value int, that doesn't work. [[self numberAttribute] intValue] == 0 // always returns no As a matter of fact, [[self numberAttribute] intValue]] returns something that looks suspiciously like a pointer to a memory address. Anyone have any

Convert CGFloat to NSNumber in Swift

妖精的绣舞 提交于 2019-12-08 14:28:00
问题 I have an extension in Swift that holds some properties which are CGFloat 's. The problem is that I don't know how to get the store the CGFloat value as a NSNumber using the associated objects Here is the code I have that doesn't work but it details what I want to do: var scaledFontSize: CGFloat { get { guard let fontSize = objc_getAssociatedObject(self, &AssociatedKeys.scaledFontSize) as? NSNumber else { //Set it let scaledFont:CGFloat = VGSizeValues.getValueFromValue(self.font.pointSize); /

How to show only the first two digits with NSNumberFormatter?

喜欢而已 提交于 2019-12-08 11:50:17
问题 I have a number for an example 1329094. I want to get only the first two digits that I make see 13 I create NSNumberFormatter like func setReceivedChart(description: [String], value: [Double]) { var descriptionString: String! var formatterNumber: NSNumber! var chartDataEntryArray = [ChartDataEntry]() for item in 0..<description.count { let chartEntry = ChartDataEntry(value: value[item], xIndex: item) chartDataEntryArray.append(chartEntry) descriptionString = description[item] formatterNumber

NSNumber double with many decimal places being rounded/truncated

試著忘記壹切 提交于 2019-12-07 10:52:47
问题 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

Core Data, NSNumber, Integer 32 and Integer 64

别来无恙 提交于 2019-12-07 10:06:44
问题 In Core Data, I have many attributes declared as Integer 64, and then accessed through NSNumber properties (this is by default). Does it matter if I store and access these values by: NSNumber *mySetValue = [NSNumber numberWithInt:someIntValue]; [myObject setMyNumberProperty:mySetValue]; int myRetrievedValue = [myObject.myNumberProperty intValue]; or by NSNumber *mySetValue = [NSNumber numberWithInteger:someIntegerValue]; [myObject setMyNumberProperty:mySetValue]; NSInteger myRetrievedValue =