Error in Xcode 6 - view controller does not have an outlet named (subview)

前端 未结 15 2085
刺人心
刺人心 2020-12-03 14:15

I just got this error in one of my apps, and after checking some other apps the same error is happening. None of my IBOutlets and IBActions are con

15条回答
  •  抹茶落季
    2020-12-03 14:44

    I had this issue with a Swift UIViewController subclass. I had removed the original view that IB created for me, and added a new view—which I was unable to connect.

    I went to the Identity inspector for the File's owner, and tried to re-enter it, thinking that maybe it had been modified somehow. Oddly, the class name did not autocomplete. It seemed like XCode was unable to actually see my class.

    I tried removing and re-adding both the .swift and .xib files, to no avail. XCode would allow me to manually type in my owning class name, but it would not autocomplete. It seemed to think it didn't exist, or wasn't valid for this context.

    Looking back at my code, I had something like the following:

    extension SomeViewController {
        func foo() -> Bool {
            return false
        }
    }
    
    class SomeViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    

    It compiles just fine, but on a hunch, I removed the extension. I saved the file, went back to the .xib, and was able to set the File's Owner identity with autocompletion again. I was also able to wire up the view.

    It would suck if extensions always broke things of course, so I tried again, this time with the extension after the class:

    class SomeViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension SomeViewController {
        func foo() -> Bool {
            return false
        }
    }
    

    Everything still worked in IB. To sanity check, I moved the extension back ahead of the class definition, and things were again horked.

    I took a look a the *-Swift.h generated by XCode, and at least relative to the affected class, there seemed to be no differences—regardless of where I put the extensions in the Swift file, they were always declared after the @interface definition for the actual class in the header.

    So, long story short, in my case this was due to Swift extensions (I say extensions because my actual code has many) coming before the class definition. I moved them after the class definition, and the problem went away.

提交回复
热议问题