我的代码正在使用swift 4.1编译Xcode 9.3, 我可以在设备上运行我的代码,但在尝试在模拟器上运行此代码时,它会给我以下错误,
我尝试了很多可用于此错误的解决方案,但它们都没有为我工作。
我的swift文件中有以下代码,导致此错误,
import Foundation
import UIKit
extension UITableView {
func register<T: UITableViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {
self.register(T.nib, forCellReuseIdentifier: T.reuseIdentifier)
}
func registerClass<T: UITableViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {
self.register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T? where T: ReusableView {
guard let cell = self.dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
}
return cell
}
}
extension UICollectionView {
func register<T: UICollectionViewCell>(_: T.Type) where T: ReusableView {
self.register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
}
// func register<T: UICollectionViewCell where T: ReusableView, T: NibLoadableView>(_: T.Type) {
// self.register(T.nib, forCellWithReuseIdentifier: T.reuseIdentifier)
// }
func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T? where T: ReusableView {
guard let cell = self.dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
}
return cell
}
}
// MARK: Add to superView with autoresizingMask(FlexibleWidth and FlexibleHeight)
extension UIView {
func getKeyboardFrame(_ keyboardNotification: Notification) -> CGRect {
guard let info = keyboardNotification.userInfo else { return CGRect(x: 0, y: 0, width: 0, height: 0) }
let value: AnyObject = info[UIKeyboardFrameEndUserInfoKey] as AnyObject
let rawFrame = value.cgRectValue
return self.convert(rawFrame!, from: nil)
}
// MARK: Get UIView from Nib
class func getNibView<T: UIView>() -> T? where T: NibLoadableView {
return Bundle.main.loadNibNamed(T.nibName, owner: self, options: nil)?[0] as? T
}
/// Add a view to a superview with the view's frame equal to the superview's bounds and the view having autoresizing mask enabled.
func add(to superView: UIView) {
self.frame = superView.bounds
self.autoresizingMask = [
.flexibleWidth,
.flexibleHeight
]
superView.addSubview(self)
}
}
我想在模拟器上运行代码,但不能这样做。
答案 0 :(得分:1)
我认为这些是真正的编译器问题。尝试进行微小的更改,直到它工作。