Xcode : Invalid redeclaration of 'ViewController'

J.BAT picture J.BAT · Oct 20, 2017 · Viewed 7.3k times · Source

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'

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)

    }

}

Answer

Rashwan L picture Rashwan L · Oct 20, 2017

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.