New project files requiring the import of UIKit

2019-03-03 03:45发布

I have a project A, which I started writing with Swift1-Xcode6. I have some extension files like:

extension UIView {
    convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) {
        self.init(frame: CGRect(x: x, y: y, width: w, height: h))
    }
}

I created a new project B and copied these extension files in there. I am getting this error:

UIViewExtensions.swift:11:11: Use of undeclared type 'UIView'

Files in Project-A do not need the inclusion of UIKit, but Project-B does. What is the reason?

2条回答
唯我独甜
2楼-- · 2019-03-03 04:36

UIView is defined in the UIKit framework, so a Swift file using that class needs to import UIKit.

But since all Swift files import the (Swift mapping of the) bridging header file, it would also be sufficient if the bridging header file directly or indirectly imports the UIKit framework.

That could be the reason why you did not have to import it explicitly in the Swift file in your older project.

查看更多
Anthone
3楼-- · 2019-03-03 04:38

This probably happens because you import some Objective-C Frameworks in your bridging header that happens to import UIKit in its .h file - which automatically imports UIKit to all your Swift classes

查看更多
登录 后发表回答