Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double

孤者浪人 提交于 2020-01-24 08:47:26

问题


Objective C:

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];

Here var1 is of type double and var2,var3 are of the type long long.

This gives me a syntax error saying that "Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double" at var1

I'm a newbie in Objective C and unable to figure out what I'm doing wrong.


回答1:


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:

Wrap all scalar types in the shortcut NSNumber initializer @()

 double var1 = 12.0;
 NSString *var2 = @"Foo";
 NSArray *valArray = [NSArray arrayWithObjects: @(var1), var2, nil];

or still shorter

 NSArray *valArray = @[@(var1), var2];

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.




回答2:


Convert var1, var2, var3 to NSNumber will resolve it.



来源:https://stackoverflow.com/questions/46884265/cannot-initialize-a-parameter-of-type-id-nonnull-with-an-lvalue-of-type-doubl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!