无法使用 double 类型的左值初始化“id _NonNull"类型的参数 [英] Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double
问题描述
目标 C:
我有多个 double、long long、NSString 和 int 类型的变量,我想将它们放入一个数组中,以便在 CSV 文件中作为单行打印
I have multiple variables of type double, long long, NSString and int which I want to put in an array to be printed as a single line in a CSV file
NSArray *ValArray = [NSArray arrayWithObjects: var1, var2, var3, var4, var5, nil];
NSArray *ValArray = [NSArray arrayWithObjects: var1, var2, var3 , var4, var5, nil];
这里 var1 是 double 类型,var2,var3 是 long long 类型.
Here var1 is of type double and var2,var3 are of the type long long.
这给了我一个语法错误,说无法在 var1 处用 double 类型的左值初始化类型为 'id _NonNull' 的参数"
This gives me a syntax error saying that "Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double" at var1
我是 Objective C 的新手,无法弄清楚我做错了什么.
I'm a newbie in Objective C and unable to figure out what I'm doing wrong.
推荐答案
Objective-C 中NSArray
(和NSDictionary
)的内容必须是对象.所有标量类型 int
double
等都不是对象.
The contents of NSArray
(and NSDictionary
) in Objective-C must be objects. All scalar types int
double
etc. are not objects.
有一个简单的解决方案:
There is an easy solution:
将所有标量类型包装在快捷方式 NSNumber
初始值设定项 @()
Wrap all scalar types in the shortcut NSNumber
initializer @()
double var1 = 12.0;
NSString *var2 = @"Foo";
NSArray *valArray = [NSArray arrayWithObjects: @(var1), var2, nil];
或者更短
NSArray *valArray = @[@(var1), var2];
要从数组中取回 double
类型,您必须编写
To get the double
type back from the array you have to write
double var3 = valArray[0].doubleValue;
附注:变量名应该以小写字母开头.
Side note: Variable names are supposed to start with a lowercase letter.
这篇关于无法使用 double 类型的左值初始化“id _NonNull"类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!