Skip to content
Advertisement

Adding a newly created object into an array in swift

I decided to start a project in java to get a rough outline of how the program was to function before I started to program it in swift(a language I am completely unfamiliar with) whilst trying to convert the code I’ve ran into an issue and cannot even understand how I’ve gone wrong. This is context for the code

JavaScript

In Swift I’ve got this far

JavaScript

but the final line

ItemsList.append(Item(name: String,price: Double))

is returning an error when running

Advertisement

Answer

The problem is here

JavaScript

Look at it JAVA code:

JavaScript

Your passing name and price as a value parameter, not a data type.

Why you get an error? It because when you call any function you need to pass value instead of Data Type

The correct syntex is :

JavaScript

If you want to JAVA type constructor syntax style, you can use underscore like this,

JavaScript

Note: Variable name must start with lower case. More: https://google.github.io/swift/

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement