Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 625
secret.shadow Avatar asked Oct 18 '25 09:10

secret.shadow


1 Answers

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.

like image 65
vadian Avatar answered Oct 20 '25 23:10

vadian