How to solve error message in my Swift Code : "Cannot call value on non-function type '[String]'

I am new to Swift programming. I have been struggling with this error in my Swift code: "Cannot call value on non-function type '[String]':

I have not been able to find any help in the Swift Resources. Please help with some direction regarding solving the error.


Here is the code:


1 override func viewDidLoad() {

2 super.viewDidLoad()

3 let persistentContainer = NSPersistentContainer(name: "No_Regrets")

4 persistentContainer.loadPersistentStores { (_, error) in

5 if let error = error {

6 fatalError("Failed to load Core Data stack: \(error)")

7 }

8 }

9 // Creates a task with a new background context created on the fly

10 persistentContainer.performBackgroundTask { (context) in

11 //iterates the array

12

13 let Gains = [self.gain1, self.gain2, self.gain3, self.gain4]

14

15 Gains.forEach {_ in

16 // Creates a new entry inside the context `context` and assign the array element `name` to the dog's name

17

18

19 let gain1 = Gains(context: context) // Cannot call value of non-function type '[String]'

20 gain1.name = name

21 let gain2 = Gains(context: context) // Cannot call value of non-function type '[String]'

22 gain2.name = name

23 let gain3 = Gains(context: context) // Cannot call value of non-function type '[String]'

24 gain3.name = name

25 let gain4 = Gains(context: context) //Cannot call value of non-function type '[String]'

26 gain4.name = name

27 }

28 do {

29 // Saves the entries created in the `forEach`

30 try context.save()

31 } catch {

32 fatalError("Failure to save context: \(error)")

33 }

34 }

You should better not use capitalized name for non-types.


Try changing `Gains` to `gains` on line 13 and 15 and see what you get.

How to solve error message in my Swift Code : "Cannot call value on non-function type '[String]'
 
 
Q