How to Resolve Swift 5 error: "Cannot Call value of non-function type '[String]'

Hello:


I have been struggling with this small piece of code foe days. Every time I resolve one problem with it, another one pops up:


I seem to have resolved the problem of "Expected Pattern" but now I have the problem "Cannot call value of non-function type '[String]'


Here is the code:



var name = [String]()


override func viewDidLoad() {

super.viewDidLoad()

let persistentContainer = NSPersistentContainer(name: "No_Regrets")

persistentContainer.loadPersistentStores { (_, error) in

if let error = error {

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

}

}

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

persistentContainer.performBackgroundTask { (context) in

//iterates the array

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

Gains.forEach {_ in

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

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

gain1.name = name

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

gain2.name = name

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

gain3.name = name

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

gain4.name = name

}

do {

// Saves the entries created in the `forEach`

try context.save()

} catch {

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

}

}

Nice of Apple to respond to this one. These forums are truly as awful as Apple's Swift documentation.

May be because the question lacked information and code was poorly formatted…

What's the point ? Have you a specific reason to reopen this 2 years old thread ?

But answer is pretty straightforward:

  • Gains (should be written gains to conform with Swift standards) is declared as Array (probably of String, but self.gain1 is declared nowhere in the posted code).
  • Using it as a func cannot work.

No need for Apple to respond nor change to Swift documentation, it is very basic Swift.

have a good day.

How to Resolve Swift 5 error: "Cannot Call value of non-function type '[String]'
 
 
Q