无法在 UIKit UI 重力方向向量中获取 CoreMotion 数据

Cant get CoreMotion data in UIKit UIGravityDirection vector

所以我正在尝试为 Swift 中的一个学校项目制作一个应用程序。 (我对这种语言完全陌生)该应用程序应该允许您倾斜您的设备,并通过这样做使屏幕底部出现方形。这是现在使用的 ip:

func gravityUpdated(){
        if let data = motionManager.deviceMotion {
            let gravity = data.gravity
            Zwaartekracht.gravityDirection = CGVectorMake(CGFloat(gravity.x), CGFloat(gravity.y))
        }

    }
   gravityUpdated()

当我 运行 它在我的 iPhone 6 上时,构建成功,但后来我看到了这个:(很抱歉post):

    libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ():
    0x100171448 <+0>:   stp    x29, x30, [sp, #-16]!
    0x10017144c <+4>:   mov    x29, sp
    0x100171450 <+8>:   sub    sp, sp, #16
    0x100171454 <+12>:  and    w8, w2, #0x1
    0x100171458 <+16>:  tbnz   w8, #0, 0x100171478       ; <+48>
    0x10017145c <+20>:  tbnz   x1, #63, 0x100171568      ; <+288>
    0x100171460 <+24>:  add    x1, x0, x1
    0x100171464 <+28>:  mov    x2, x3
    0x100171468 <+32>:  mov    x3, x4
    0x10017146c <+36>:  mov    x4, x5
    0x100171470 <+40>:  bl     0x1001a1334               ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
->  0x100171474 <+44>:  brk    #0x1
    0x100171478 <+48>:  str    xzr, [sp, #8]
    0x10017147c <+52>:  cmp    x0, w0, uxtw
    0x100171480 <+56>:  b.ne   0x100171620               ; <+472>
    0x100171484 <+60>:  lsr    w8, w0, #11
    0x100171488 <+64>:  cmp    w8, #27
    0x10017148c <+68>:  b.ne   0x1001714b4               ; <+108>
    0x100171490 <+72>:  adr    x0, #584602               ; "fatal error"
    0x100171494 <+76>:  nop    
    0x100171498 <+80>:  adr    x3, #586600               ; "high- and low-surrogate code points are not valid Unicode scalar values"
    0x10017149c <+84>:  nop    
    0x1001714a0 <+88>:  movz   w1, #0xb
    0x1001714a4 <+92>:  orr    w2, wzr, #0x2
    0x1001714a8 <+96>:  movz   w4, #0x47
    0x1001714ac <+100>: orr    w5, wzr, #0x2
    0x1001714b0 <+104>: bl     0x100171448               ; <+0>
    0x1001714b4 <+108>: lsr    w10, w0, #16
    0x1001714b8 <+112>: cmp    w10, #16
    0x1001714bc <+116>: b.hi   0x1001714ec               ; <+164>
    0x1001714c0 <+120>: cmp    w0, #128
    0x1001714c4 <+124>: b.lo   0x100171510               ; <+200>
    0x1001714c8 <+128>: orr    w8, wzr, #0x80
    0x1001714cc <+132>: bfxil  x8, x0, #0, #6
    0x1001714d0 <+136>: cmp    w0, #2047
    0x1001714d4 <+140>: b.hi   0x10017158c               ; <+324>
    0x1001714d8 <+144>: movz   x11, #0
    0x1001714dc <+148>: movz   x10, #0
    0x1001714e0 <+152>: lsr    w9, w0, #6
    0x1001714e4 <+156>: orr    w9, w9, #0xffffffc0
    0x1001714e8 <+160>: b      0x1001715e8               ; <+416>
    0x1001714ec <+164>: adr    x0, #584510               ; "fatal error"
    0x1001714f0 <+168>: nop    
    0x1001714f4 <+172>: adr    x3, #586588               ; "value is outside of Unicode codespace"
    0x1001714f8 <+176>: nop    
    0x1001714fc <+180>: movz   w1, #0xb
    0x100171500 <+184>: orr    w2, wzr, #0x2
    0x100171504 <+188>: movz   w4, #0x25
    0x100171508 <+192>: orr    w5, wzr, #0x2
    0x10017150c <+196>: bl     0x100171448               ; <+0>
    0x100171510 <+200>: movz   x10, #0
    0x100171514 <+204>: movz   x9, #0
    0x100171518 <+208>: mov    x8, x0
    0x10017151c <+212>: orr    w11, wzr, #0x8
    0x100171520 <+216>: umulh  x11, x9, x11
    0x100171524 <+220>: cmp    xzr, x11
    0x100171528 <+224>: cset   w11, ne
    0x10017152c <+228>: tbnz   w11, #0, 0x100171620      ; <+472>
    0x100171530 <+232>: lsl    x11, x9, #3
    0x100171534 <+236>: cmp    x11, #63
    0x100171538 <+240>: b.hi   0x100171624               ; <+476>
    0x10017153c <+244>: and    x8, x8, #0xff
    0x100171540 <+248>: lsl    x8, x8, x11
    0x100171544 <+252>: orr    x8, x8, x10
    0x100171548 <+256>: str    x8, [sp, #8]
    0x10017154c <+260>: cmn    x9, #2
    0x100171550 <+264>: b.le   0x100171568               ; <+288>
    0x100171554 <+268>: add    x8, sp, #8
    0x100171558 <+272>: add    x8, x9, x8
    0x10017155c <+276>: add    x1, x8, #1
    0x100171560 <+280>: add    x0, sp, #8
    0x100171564 <+284>: b      0x100171464               ; <+28>
    0x100171568 <+288>: adr    x0, #584386               ; "fatal error"
    0x10017156c <+292>: nop    
    0x100171570 <+296>: adr    x3, #584656               ; "UnsafeBufferPointer with negative count"
    0x100171574 <+300>: nop    
    0x100171578 <+304>: movz   w1, #0xb
    0x10017157c <+308>: orr    w2, wzr, #0x2
    0x100171580 <+312>: movz   w4, #0x27
    0x100171584 <+316>: orr    w5, wzr, #0x2
    0x100171588 <+320>: bl     0x100171448               ; <+0>
    0x10017158c <+324>: orr    w9, wzr, #0xffffff80
    0x100171590 <+328>: bfxil  w9, w0, #6, #6
    0x100171594 <+332>: cbnz   w10, 0x1001715ac          ; <+356>
    0x100171598 <+336>: movz   x11, #0
    0x10017159c <+340>: movz   x12, #0
    0x1001715a0 <+344>: lsr    w10, w0, #12
    0x1001715a4 <+348>: orr    w10, w10, #0xffffffe0
    0x1001715a8 <+352>: b      0x1001715d0               ; <+392>
    0x1001715ac <+356>: lsr    w10, w0, #18
    0x1001715b0 <+360>: orr    w11, w10, #0xf0
    0x1001715b4 <+364>: cmp    w11, w11, uxtb
    0x1001715b8 <+368>: b.ne   0x100171620               ; <+472>
    0x1001715bc <+372>: orr    w10, wzr, #0xffffff80
    0x1001715c0 <+376>: bfxil  w10, w0, #12, #6
    0x1001715c4 <+380>: and    x12, x11, #0xff
    0x1001715c8 <+384>: str    x12, [sp, #8]
    0x1001715cc <+388>: orr    w11, wzr, #0x1
    0x1001715d0 <+392>: and    x10, x10, #0xff
    0x1001715d4 <+396>: lsl    x13, x11, #3
    0x1001715d8 <+400>: lsl    x10, x10, x13
    0x1001715dc <+404>: orr    x10, x10, x12
    0x1001715e0 <+408>: str    x10, [sp, #8]
    0x1001715e4 <+412>: add    x11, x11, #1
    0x1001715e8 <+416>: orr    w12, wzr, #0x8
    0x1001715ec <+420>: umulh  x12, x11, x12
    0x1001715f0 <+424>: cmp    xzr, x12
    0x1001715f4 <+428>: cset   w12, ne
    0x1001715f8 <+432>: tbnz   w12, #0, 0x100171620      ; <+472>
    0x1001715fc <+436>: lsl    x12, x11, #3
    0x100171600 <+440>: cmp    x12, #64
    0x100171604 <+444>: b.hs   0x100171624               ; <+476>
    0x100171608 <+448>: and    x9, x9, #0xff
    0x10017160c <+452>: lsl    x9, x9, x12
    0x100171610 <+456>: orr    x10, x9, x10
    0x100171614 <+460>: str    x10, [sp, #8]
    0x100171618 <+464>: add    x9, x11, #1
    0x10017161c <+468>: tbz    x9, #63, 0x10017151c      ; <+212>
    0x100171620 <+472>: brk    #0x1
    0x100171624 <+476>: adr    x0, #584198               ; "fatal error"
    0x100171628 <+480>: nop    
    0x10017162c <+484>: adr    x3, #584228               ; "shift amount is larger than type size in bits"
    0x100171630 <+488>: nop    
    0x100171634 <+492>: movz   w1, #0xb
    0x100171638 <+496>: orr    w2, wzr, #0x2
    0x10017163c <+500>: movz   w4, #0x2d
    0x100171640 <+504>: orr    w5, wzr, #0x2
    0x100171644 <+508>: bl     0x100171448               ; <+0>

底部还写着:fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

我完全不知道这是怎么发生的

有人可以帮忙吗?谢谢!

使用此代码使其工作:

func gravityUpdated(motion: CMDeviceMotion!, error: NSError!) {
    let grav : CMAcceleration = motion.gravity;

    let x = CGFloat(grav.x);
    let y = CGFloat(grav.y);
    var p = CGPointMake(x,y)



    // Have to correct for orientation.
    var orientation = UIApplication.sharedApplication().statusBarOrientation;

    if(orientation == UIInterfaceOrientation.LandscapeLeft) {
        var t = p.x
        p.x = 0 - p.y
        p.y = t
    } else if (orientation == UIInterfaceOrientation.LandscapeRight) {
        var t = p.x
        p.x = p.y
        p.y = 0 - t
    } else if (orientation == UIInterfaceOrientation.PortraitUpsideDown) {
        p.x *= -1
        p.y *= -1
    }

    var v = CGVectorMake(p.x, 0 - p.y);
    gravity.gravityDirection = v;
    animator.addBehavior(gravity)



}