Invalid Redeclaration of ****

swift Invalid Redeclaration

That error message means that you have created two functions with the same name.

Sample Image

You can not use same name and same signature for function. Yes function overloading is there and it means that you can use same name with different parameters. You can create as many function as you want using same name. The thumb rule is each overloading function must have different parameters.

For Example:

func dropShape() {        
}

func dropShape(points: CGPoint) {
}

Invalid redeclaration of ****

  • Go your project root directory->Build phases.
  • Under complied resources, check AppDelegate file is added twice or not.

If it is added twice, then added one file.

Invalid redeclaration of 'rollButtonPressed'

I'm not sure why you want two same IBActions, but from the image, I can see there are two same IBAction functions, so remove one or rename one of them.

Invalid redeclaration compiler error on generic type extension

That is a known bug/restriction, see SR-8123 – Invalid redeclaration of name when creating a func name() in a different extension of the same type with different where clause:

However, the compiler fires an invalid redeclaration error if an extension defines a function with the same name as a variable defined in another extension.

and

I think we should make this legal – currently we're always considering variables to conflict with non-variables (and functions with non-functions) if their overload signatures conflict, regardless of the generic environment. But if the generic environment differs, IMO it's reasonable to allow to allow the overload.

Invalid redeclaration of 'numberOfComponents(in:)' error

Your picker view code is wrong. First, I would recommend changing the names of them from pickerView and pickerViewTwo to something more descriptive.

The required functions for picker view can't be used multiple times, thus you are getting the error. The proper way to do it is:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent
component: Int) -> String? {

//After renaming the first pickerView to topPickerView
if pickerView == topPickerView {
return pickerData[row]
}
else {
return pickerDataTwo[row]
}
}

There should be an if-else in each of the required functions, with the if statement saying that if pickerView == oneOfThePickerViewsName, with the if else statement. Whats happening is when the pickers are being set up, it checks which picker it is and uses the code for that.
Here is how the rest should be formatted:

//pickerView has been renamed to topPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
//You wouldn't need to do this here as its the same for both of them.
//
if pickerView == topPickerView
{
return 1
}
else
{
return 1
}
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == topPickerView
{
return pickerData.count
}
else
{
return pickerdataTwo.count
// should actually be formatted pickerDataTwo because of camelCase.
}
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent
component: Int) -> String? {
if pickerView == topPickerView
{
return pickerData[row]
}
else
{
return pickerdataTwo[row]
// should actually be formatted pickerDataTwo because of camelCase.
}
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent
component: Int) {
if pickerView == topPickerView
{
label.text = pickerData[row]
}
else
{
labelTwo.text = pickerdataTwo[row]
// should actually be formatted pickerDataTwo because of camelCase.
}
}

Error in 2nd ViewController Invalid redeclaration of 'doneAccessory'

Please add this UITexfield extension to your helper class. Its good practice having all your extensions in the helper class.

You need not set anything about your second view controller. You need to open your storyboard and select the text field where you want the done button. Find the image to set the done button then you will not face any issue.

Storyboard settings

Invalid redeclaration of 'variable.storage' in Swift 4.2 after updating to Xcode 10.2

I had the same errors, which also masked some other errors. Once I resolved the other errors, the Invalid redeclaration of 'variable.storage' didn't occur anymore.

Invalid redeclaration of 'mapView(_:viewFor:)'

This normally happens when you already implemented that method elsewhere.

Check your ViewController to make sure you don't have func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {} implemented outside of that extension.



Related Topics



Leave a reply



Submit