[英]What is the difference between @“1.5” and @(1.5) while setting the value in NSDictionary?
In iPhone project, 在iPhone专案中,
It was while I was while setting Value in dictionary, 当时我在字典中设定Value
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc] init];
[dictionary setValue:@(2.8) forKey:@"Why"];
AND, 和,
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc] init];
[dictionary setValue:@"2.8" forKey:@"Why"];
My question is Why not @"2.5" and @(2.5) ? 我的问题是为什么不@@ 2.5和@(2.5)?
You have two questions, it would be better to have a single question. 您有两个问题,最好有一个问题。
But as to the difference, @"2.5"
is an NSString
where @(2.5)
is an NSNumber
. 但关于区别,
@"2.5"
是一个NSString
,其中@(2.5)
是一个NSNumber
。 There is a big difference between textual and numeric data. 文本数据和数字数据之间存在很大差异。
As for why you need an NSNumber
and not NSString
is obvious: the kerning is a numeric value. 至于为什么需要
NSNumber
而不是NSString
原因很明显:字距调整是一个数字值。
using the @() syntax you can box arbitrary C expressions. 使用@()语法,您可以将任意C表达式装箱。 This makes it trivial to turn basic arithmetic calculations into
NSNumber
objects see below: 这使得将基本算术计算转换为
NSNumber
对象变得很简单,请参见以下内容:
double x = 24.0;
NSNumber *result = @(x * .15);
NSLog(@"%.2f", [result doubleValue]);
You can also refer NSNumber
object as @"" string but cant make calculations like above example. 您也可以将
NSNumber
对象引用为@“”字符串,但不能像上面的示例那样进行计算。 In your case both are acceptable but here calculation makes difference. 在您的情况下,两者都是可以接受的,但此处的计算有所不同。
When you use 使用时
@"2.5" it's behave like a string @“ 2.5”就像一个字符串
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc] init];
[dictionary setValue:@"2.8" forKey:@"Why"];;
NSString *a = [dictionary ValueforKey:@"Why"];
but when you use @(2.8) then it's behave like a NSNumber 但是当您使用@(2.8)时,它的行为就像一个NSNumber
NSMutableDictionary*dictionary=[[NSMutableDictionary alloc] init];
[dictionary setValue:@(2.8) forKey:@"Why"];;
NSNumber *a = [dictionary ValueforKey:@"Why"];
@(2.8) is a type of NSNumber. @(2.8)是NSNumber的一种。
@"2.8" is a type of NSString. @“ 2.8”是NSString的一种。
Both the type and value were different between there two. 两者之间的类型和值都不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.