Swift xcode中的EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)
我正在尝试为我的游乐场创建集合视图类。我有这个密码。当我在Xcode游乐场中创建UICollectionView时,它是一个类,之后它将在我的主体文件中使用 这是我的代码:Swift xcode中的EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0),swift,swift-playground,Swift,Swift Playground,我正在尝试为我的游乐场创建集合视图类。我有这个密码。当我在Xcode游乐场中创建UICollectionView时,它是一个类,之后它将在我的主体文件中使用 这是我的代码: import UIKit import SceneKit public class MenuCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlow
import UIKit
import SceneKit
public class MenuCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public init(frame: CGRect) {
let layout = UICollectionViewFlowLayout()
super.init(frame: frame, collectionViewLayout: layout)
self.dataSource = self
self.delegate = self
self.backgroundColor = UIColor(red: 0, green: 0, blue: 38, alpha:1.0)
self.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "MenuCell")
}
required public init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuCell", for: indexPath) as! MenuCollectionViewCell
cell.PlanetName.text = Constants.names[indexPath.row]
cell.PlanetImage.image = UIImage(named: Constants.imagesNames[indexPath.row])?.cropToSquare().cropToCircle()
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.frame.size.width/2)-10, height: (((self.frame.size.width/2)-10)*1.4)+25)
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! MenuCollectionViewCell
self.tag = indexPath.row
UIView.animate(
withDuration: 0.3,
delay: 0.0,
usingSpringWithDamping: 1.1,
initialSpringVelocity: 1.5,
options: UIViewAnimationOptions.curveEaseInOut,
animations: ({
cell.PlanetImage.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}), completion: {finished in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MenuItemSelected"), object: nil)
UIView.animate(
withDuration: 0.3,
delay: 0.0,
usingSpringWithDamping: 1.1,
initialSpringVelocity: 1.5,
options: UIViewAnimationOptions.curveEaseInOut,
animations: ({
cell.PlanetImage.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}), completion: nil)
})
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 5, 0, 5)
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
}
使用下一行代码时会出现该错误:
let menuView1 = MenuCollectionView(frame: CGRect.init(x: -(viewWidth/2.2), y: 0.0, width: viewWidth/2.2, height: viewHeight))
运行项目时出现的错误是:
EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)。进程被留在中断的位置,请使用“thread return-x”返回到表达式求值之前的状态 我的Xcode版本是9.2,我使用Swift 4。如果您有任何问题,请发表评论,我将提供有关错误的更多信息。
你能发布你在调试区域看到的内容吗?@Xcoder我在我的出版物中发布了一个调试器数据的捕获