(SWIFT)错误:操场执行中止:错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP, subcode=0x0)

时间:2023-01-23 19:12:25

I'm getting this error code

我得到了这个错误代码

error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

错误:操场执行中止:错误:执行被中断,原因:exc_bad_instructions(代码=EXC_I386_INVOP,子代码=0x0)。这个过程在被中断的时候,使用“线程返回-x”返回到状态,然后再进行表达式评估。

Here's my code:

这是我的代码:

import Cocoa
import SpriteKit
import PlaygroundSupport

public class Scene: SKScene {

var point = CGPoint(x: 0,y : 0)
let viewFrame = CGRect(x:0,y:0, width: 1080, height: 1080)

override init(size: CGSize) {

    super.init(size: size)
            //these 3 lines are the problem

    let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeInKeyWindow] as NSTrackingAreaOptions
    let tracker = NSTrackingArea(rect: view, options: options, owner: self.view, userInfo: nil)
    self.view?.addTrackingArea(tracker)               //print(self.view!)

    //Intro
    let welcomeLabel = SKLabelNode(text: "Music Box")
    welcomeLabel.fontColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    welcomeLabel.position = CGPoint(x: size.width / 2, y: size.height/2)
    welcomeLabel.fontSize = 150

    addChild(welcomeLabel)

    delay(3.0)
    {
        let fadeOutAction = SKAction.fadeAlpha(to: 0.0, duration: 1.0)
        welcomeLabel.run(fadeOutAction)
        self.demo()
    }
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func demo() {
    let songLabel = SKLabelNode(text: "song - SONG NAME")
    songLabel.fontColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    songLabel.position = CGPoint(x: size.width / 2, y: size.height/12)
    songLabel.fontSize = 50
    songLabel.alpha = 0.0

    addChild(songLabel)

    let fadeInAction = SKAction.fadeAlpha(to: 1.0, duration: 1.0)

    songLabel.run(fadeInAction)
    self.musicBox()
}

func musicBox() {

    let body = SKSpriteNode(imageNamed: "musicBox")
    body.size = CGSize(width: 845.0, height: 379.0)
    body.position = CGPoint(x: 535, y: 454)

    let crank = SKSpriteNode(imageNamed: "crank")
    crank.size = CGSize(width: 155.0, height: 188.0)
    crank.position = CGPoint(x: 835.0, y: 480)
    crank.anchorPoint = CGPoint(x: 0.5 , y: 0.5-0.08)

    addChild(body)
    addChild(crank)

    print(point)

    crank.run(
        SKAction.repeatForever (
            SKAction.sequence([
                SKAction.wait(forDuration: 0.1),
                SKAction.run({

                    let angle = atan2(self.point.y - crank.position.y , self.point.x - crank.position.x)
                    let rotate = SKAction.rotate(toAngle: angle - CGFloat(M_PI_2), duration: 0.25)
                    crank.run(rotate)
                    if(!(crank.frame.contains(self.point))) {
                        self.removeAction(forKey: "New Thread")
                    }
                })
                ])
        ),
        withKey: "New Thread"
    )

} 

override public func mouseMoved(with event: NSEvent) {

    point = event.location(in: self)
    print(point)
}


}

//Delays Animations
func delay(_ delay:Double, closure:@escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

let scene = Scene(size: CGSize(width: 1080, height: 1080))
scene.backgroundColor = #colorLiteral(red: 0.4078431373, green: 0.7843137255, blue: 0.6509803922, alpha: 1)
scene.scaleMode = .aspectFit

print(Thread.callStackSymbols)

let view = SKView(frame: NSRect(x: 0, y: 0, width: 1920, height: 1080))

PlaygroundPage.current.needsIndefiniteExecution = true
view.presentScene(scene)
PlaygroundPage.current.liveView = view

The error shows up on this line:

错误出现在这一行:

let scene = Scene(size: CGSize.init(width: 1080, height: 1080))

The error seems to be caused by when I declare/use my point var, but I have no clue why this would be the case.

错误似乎是在我声明/使用我的点var时引起的,但是我不知道为什么会是这样。

Stack thread:

线程栈:

 Playground execution failed: error: MusicBox WWDC17.playground:10:44: error: cannot convert value of type 'SKView?' to expected argument type 'NSRect' (aka 'CGRect')
        let tracker = NSTrackingArea(rect: view, options: options, owner: self.view, userInfo: nil)
                                           ^~~~


* thread #1: tid = 0xb837, 0x0000000101cc9360 com.apple.dt.Xcode.PlaygroundStub-macosx`executePlayground, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000101cc9360 com.apple.dt.Xcode.PlaygroundStub-macosx`executePlayground
    frame #1: 0x0000000101cc722a com.apple.dt.Xcode.PlaygroundStub-macosx`-[PlaygroundViewBridgeService execute] + 90
    frame #2: 0x00007fff9ced43ec CoreFoundation`__invoking___ + 140
    frame #3: 0x00007fff9ced4271 CoreFoundation`-[NSInvocation invoke] + 289
    frame #4: 0x00007fff9ceed7a6 CoreFoundation`-[NSInvocation invokeWithTarget:] + 54
    frame #5: 0x00007fffb05d395b ViewBridge`__68-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:]_block_invoke + 47
    frame #6: 0x00007fffb05f2f4c ViewBridge`__runAnimationGroup_block_invoke + 21
    frame #7: 0x00007fff9b350be2 AppKit`+[NSAnimationContext runAnimationGroup:] + 58
    frame #8: 0x00007fff9aa1e782 AppKit`+[NSAnimationContext runAnimationGroup:completionHandler:] + 85
    frame #9: 0x00007fffb05f2f28 ViewBridge`runAnimationGroup + 303
    frame #10: 0x00007fffb05d3462 ViewBridge`+[NSVB_View _animateWithAttributes:animations:completion:] + 507
    frame #11: 0x00007fffb05d38dd ViewBridge`-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:] + 156
    frame #12: 0x00007fff9ced2daa CoreFoundation`___forwarding___ + 538
    frame #13: 0x00007fff9ced2b08 CoreFoundation`__forwarding_prep_0___ + 120
    frame #14: 0x00007fff9ced43ec CoreFoundation`__invoking___ + 140
    frame #15: 0x00007fff9ced4271 CoreFoundation`-[NSInvocation invoke] + 289
    frame #16: 0x00007fff9ceed7a6 CoreFoundation`-[NSInvocation invokeWithTarget:] + 54
    frame #17: 0x00007fffb05a135d ViewBridge`-[NSVB_QueueingProxy forwardInvocation:] + 327
    frame #18: 0x00007fff9ced2daa CoreFoundation`___forwarding___ + 538
    frame #19: 0x00007fff9ced2b08 CoreFoundation`__forwarding_prep_0___ + 120
    frame #20: 0x00007fff9ced43ec CoreFoundation`__invoking___ + 140
    frame #21: 0x00007fff9ced4271 CoreFoundation`-[NSInvocation invoke] + 289
    frame #22: 0x00007fff9ceed7a6 CoreFoundation`-[NSInvocation invokeWithTarget:] + 54
    frame #23: 0x00007fff9ced2daa CoreFoundation`___forwarding___ + 538
    frame #24: 0x00007fff9ced2b08 CoreFoundation`__forwarding_prep_0___ + 120
    frame #25: 0x00007fff9ced43ec CoreFoundation`__invoking___ + 140
    frame #26: 0x00007fff9ced4271 CoreFoundation`-[NSInvocation invoke] + 289
    frame #27: 0x00007fff9ceed7a6 CoreFoundation`-[NSInvocation invokeWithTarget:] + 54
    frame #28: 0x00007fff9ced2daa CoreFoundation`___forwarding___ + 538
    frame #29: 0x00007fff9ced2b08 CoreFoundation`__forwarding_prep_0___ + 120
    frame #30: 0x00007fff9ced43ec CoreFoundation`__invoking___ + 140
    frame #31: 0x00007fff9ced4271 CoreFoundation`-[NSInvocation invoke] + 289
    frame #32: 0x00007fffb05f752b ViewBridge`withHintInProgress + 487
    frame #33: 0x00007fffb05f725a ViewBridge`__deferNSXPCInvocationOntoMainThread_block_invoke_2 + 233
    frame #34: 0x00007fffb0578df2 ViewBridge`+[NSViewServiceApplication withHostPID:invoke:] + 46
    frame #35: 0x00007fffb05f711d ViewBridge`__deferNSXPCInvocationOntoMainThread_block_invoke + 179
    frame #36: 0x00007fffb05f6d0a ViewBridge`__deferBlockOntoMainThread_block_invoke_2 + 544
    frame #37: 0x00007fff9cef67dc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #38: 0x00007fff9ced77e4 CoreFoundation`__CFRunLoopDoBlocks + 356
    frame #39: 0x00007fff9ced7326 CoreFoundation`__CFRunLoopRun + 1878
    frame #40: 0x00007fff9ced6974 CoreFoundation`CFRunLoopRunSpecific + 420
    frame #41: 0x00007fff9c462a5c HIToolbox`RunCurrentEventLoopInMode + 240
    frame #42: 0x00007fff9c462891 HIToolbox`ReceiveNextEventCommon + 432
    frame #43: 0x00007fff9c4626c6 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 71
    frame #44: 0x00007fff9aa085b4 AppKit`_DPSNextEvent + 1120
    frame #45: 0x00007fff9b182d6b AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2789
    frame #46: 0x00007fffb057a06a ViewBridge`-[NSViewServiceApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 102
    frame #47: 0x00007fff9a9fcf35 AppKit`-[NSApplication run] + 926
    frame #48: 0x00007fff9a9c7850 AppKit`NSApplicationMain + 1237
    frame #49: 0x00007fffb26888c7 libxpc.dylib`_xpc_objc_main + 775
    frame #50: 0x00007fffb26872e4 libxpc.dylib`xpc_main + 494
    frame #51: 0x00007fffb05a57eb ViewBridge`-[NSXPCSharedListener resume] + 16
    frame #52: 0x00007fffb057eeb9 ViewBridge`NSViewServiceApplicationMain + 3022
    frame #53: 0x0000000101cc93a2 com.apple.dt.Xcode.PlaygroundStub-macosx`main + 34
    frame #54: 0x00007fffb2424255 libdyld.dylib`start + 1

1 个解决方案

#1


1  

I found the solution. This line:

let tracker = NSTrackingArea(rect: view, options: options, owner: self.view, userInfo: nil)

Should actually be:

应该是:

let tracker = NSTrackingArea(rect: CGRect(x:0,y:0, width: 1080, height: 1080), options: options, owner: self.view, userInfo: nil)

I assigned the rect to an SKScene, not a CGRect; such a silly mistake.

我把rect分配给了一个SKScene,而不是CGRect;这么愚蠢的错误。

#1


1  

I found the solution. This line:

let tracker = NSTrackingArea(rect: view, options: options, owner: self.view, userInfo: nil)

Should actually be:

应该是:

let tracker = NSTrackingArea(rect: CGRect(x:0,y:0, width: 1080, height: 1080), options: options, owner: self.view, userInfo: nil)

I assigned the rect to an SKScene, not a CGRect; such a silly mistake.

我把rect分配给了一个SKScene,而不是CGRect;这么愚蠢的错误。