Xcode:“ViewController”的重新声明无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46842613/
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
Xcode : Invalid redeclaration of 'ViewController'
提问by J.BAT
I just finished my app and it was working fine, but all of a sudden after pressing File -> duplicate, I have a message saying
我刚刚完成了我的应用程序并且它工作正常,但是在按下文件 -> 复制后突然间,我有一条消息说
Invalid redeclaration of 'ViewController'
'ViewController' 的重新声明无效
Please help me out if you can guys? Thank you
如果可以的话请帮帮我好吗?谢谢
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var slideScrollView: UIScrollView!
@IBOutlet var pageControl: UIPageControl!
override func viewDidLoad() {
slideScrollView.delegate = self
let slides:[Slide] = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
}
func createSlides () -> [Slide] {
let slide1: Slide = Bundle.main.loadNibNamed("Slide", owner:self, options:nil)?.first as!Slide
slide1.label.text = "Slide1"
let slide2: Slide = Bundle.main.loadNibNamed("Slide", owner:self, options:nil)?.first as!Slide
slide2.label.text = "Slide2"
return [slide1, slide2]
}
func setupSlideScrollView (slides:[Slide]) {
slideScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
slideScrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
slideScrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
slideScrollView.addSubview(slides[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
}
回答by Rashwan L
This error means that you have another file declared with:
此错误意味着您有另一个文件声明为:
class ViewController...
You might have changed the filename, for example to ViewControllerTwo.swift but make sure to change the class declaration too.
您可能已更改文件名,例如更改为 ViewControllerTwo.swift 但请确保也更改类声明。
回答by Glenn
Did you press saveafter clicking the duplicate? The message is already telling you a solution. You need to find the other class you have that has the name ViewController
.
单击重复后是否按保存?该消息已经告诉您一个解决方案。您需要找到具有 name 的另一个类。ViewController
Two steps may help:
两个步骤可能会有所帮助:
- Search
ViewController
in your Xcode's Find Navigator(search) - Browse your project folder and search for the duplicated .swift file.
- 搜索
ViewController
在Xcode的查找导航(搜索) - 浏览您的项目文件夹并搜索重复的 .swift 文件。