Invalid redeclaration of ****

Note: I've search a lot, such as Invalid redeclaration of UITableViewDataSource method? not apply to my case. For there is no duplicate file or class in my code.

I've only one AppDelegate.swift file in my project and only one class AppDelegate in the file. But I've got

enter image description here

I also have below code in the AppDelegate.swift file. I get below error.

extension Data {
    var hexString: String {
        return withUnsafeBytes {(bytes: UnsafePointer<UInt8>) -> String in
            let buffer = UnsafeBufferPointer(start: bytes, count: count)
            return buffer.map {String(format: "%02hhx", $0)}.reduce("", { $0 + $1 })
        }
    }
}

enter image description here

Note, I've grep the duplicate class or variable declaration in my project, and there is once declaration in the code.


  • 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.


I add this answer as a supplement of pkc456's answer.

Both redeclaration and recompile can cause the issue.

In my code there is no redeclaration, so it's the recompile cause it.

I go to the Build phases menu, and I found the AppDelegate.swift file added twice there.

enter image description here

Solution:

  1. Select either of the two AppDelegate.swift file, and click the - to remove it.

  2. Clean and build the project(For Xcode Version 9.3 (9E145) Product->Clean and Product->Build).