ios 快速无效的重新声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27311043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:55:05  来源:igfitidea点击:

swift Invalid Redeclaration

iosswift

提问by mkayaa93

func dropShape() {
        if let shape = fallingShape {
            while detectIllegalPlacement() == false {
                shape.lowerShapeByOneRow()
            }
            shape.raiseShapeByOneRow()
            delegate?.gameShapeDidDrop(self)
        }
    }

Hi, I'm taking this Invalid redeclaration of 'dropShape()' so what did I wrong. Can anybody help me

嗨,我正在接受这个无效的 'dropShape()' 重新声明,所以我做错了什么。有谁能够帮助我

回答by Leo Dabus

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

该错误消息意味着您创建了两个同名的函数。

enter image description here

在此处输入图片说明

You can not use same name and same signature for function. Yes function overloadingis 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) {        
}

回答by J. Koush

I had the same issue, I've solved it by deleting an extra file in the compile sources.

我遇到了同样的问题,我通过删除编译源中的一个额外文件来解决它。

  1. Go your project root directory.
  2. Go to Build Phases.
  3. Click on Compile Sources, check for a file that has been added twice and delete one of them.
  1. 转到您的项目根目录。
  2. 转到构建阶段
  3. 单击Compile Sources,检查已添加两次的文件并删除其中一个。

That should solve your problem.

那应该可以解决您的问题。

回答by Honey

I had this exact error message just right now. For me it was a classand a structconflict.

我现在就收到了这个确切的错误消息。对我来说,这是一个和一个结构冲突。

For any two declaration of types in the samescope, you will get an error e.g. if you use anyof declare any of the 2 types below, you will get an error

对于同一范围内的任意两个类型声明,您将收到错误,例如,如果您使用以下 2 种类型中的任何一个声明,您将收到错误

class employee{...}
struct employee{...}
func employee(){...}
protocol employee{...}

It's not just for classes, structs or func, it's for everything because funcs, structs, enums, protocols are all First class citizensin Swift

它不仅适用于类、结构体或 func,它适用于一切,因为funcs, structs, enums, protocols 都是Swift 中的一等公民

回答by Faris

This error happened when you don't import UIKit and the class name should be unique so make sure to add import UIKitand the functions or class has a unique name.

当您不导入 UIKit 并且类名应该是唯一的时会发生此错误,因此请确保添加import UIKit并且函数或类具有唯一的名称。