Import of CoreMotion ignored in swift

Asked 2 years ago, Updated 2 years ago, 46 views

I have a question about swift.
This time, I wrote the code for the following site to retrieve the value of the acceleration sensor using the CoreMotion framework, but the following warning message appears in the import CoreMotion section and cannot be imported.

File 'ViewController.swift is part of module 'CoreMotion'; ignore import'.

Reference Site
https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/5-coremotion/001-jia-su-dusensano-zhiwo-qu-de

By the way, CoreMotion.framework was added from Linked Framework and Libraries.
Also, I will write the code below just in case.

import UIKit
import CoreMotion

classViewController:UIViewController {

    var myMotionManager —CMMotionManager!

    override func viewDidLoad(){

        super.viewDidLoad()

        // Create a Label.
        let myXLabel: UILabel = UILabel (frame: CGRectMake(0,0,200,50))
        myXLabel.backgroundColor=UIColor.blueColor()
        myXLabel.layer.masksToBounds=true
        myXLabel.layer.cornerRadius=10.0
        myXLabel.textColor=UIColor.whiteColor()
        myXLabel.shadowColor=UIColor.grayColor()
        myXLabel.textAlignment=NSTextAlignment.Center
        myXLabel.layer.position = CGPoint (x:self.view.bounds.width/2, y:200)

        let myYLabel: UILabel = UILabel (frame: CGRectMake(0,0,200,50))
        myYLabel.backgroundColor=UIColor.orangeColor()
        myYLabel.layer.masksToBounds=true
        myYLabel.layer.cornerRadius=10.0
        myYLabel.textColor=UIColor.whiteColor()
        myYLabel.shadowColor=UIColor.grayColor()
        myYLabel.textAlignment=NSTextAlignment.Center
        myYLabel.layer.position = CGPoint (x:self.view.bounds.width/2, y:280)

        let myZLabel: UILabel = UILabel (frame: CGRectMake(0,0,200,50))
        myZLabel.backgroundColor=UIColor.redColor()
        myZLabel.layer.masksToBounds=true
        myZLabel.layer.cornerRadius=10.0
        myZLabel.textColor=UIColor.whiteColor()
        myZLabel.shadowColor=UIColor.grayColor()
        myZLabel.textAlignment=NSTextAlignment.Center
        myZLabel.layer.position = CGPoint (x:self.view.bounds.width/2, y:360)

        // Turn the background color of the View to blue.
        self.view.backgroundColor=UIColor.cyanColor()

        // Added Label to View.
        self.view.addSubview(myXLabel)
        self.view.addSubview(myYLabel)
        self.view.addSubview(myZLabel)

        // Generate MotionManager.
        myMotionManager=CMMotionManager()

        // Set the update cycle.
        myMotionManager.accelerometerUpdateInterval=0.1

        // Start obtaining acceleration.
        myMotionManager.startAccelerometerUpdateToQueue(NSOperationQueue.mainQueue(), withHandler: {(accelerometerData!, error:NSError!) ->Void in
            myXLabel.text="x=\(accelerometerData.acceleration.x)"
            myYLabel.text="y=\(accelerometerData.acceleration.y)"
            myZLabel.text="z=\(accelerometerData.acceleration.z)"
        })

    }

}

ios swift

2022-09-30 21:11

2 Answers

When I created a new project named CoreMotion and wrote import CoreMotion, I received a warning message written in your question.Swift treats project names like a kind of framework name, so CoreMotion represents the project itself.

If the above applies, create a project under a different name (such as MyCoreMotion) and paste the same code to try it.


2022-09-30 21:11

In Xcode 7.2.1, I created a project from Single View Application, pasted the code in the questionnaire, and compiled it as it was, but there was no error in the import CoreMotion section. *We are not adding framework

The reason for the error in your environment is that the framework you are adding is wrong.Why don't you remove all the frameworks from Linked Framework and Libraries and then build them?

Beyond the main topic, if you compile the code in the questionnaire as it is, you will encounter a compilation error when you start getting acceleration.

If you modify this part, it looks like this.

// Start getting acceleration.
    // Modifying Optional Handling, etc.
    myMotionManager.startAccelerometerUpdateToQueue(NSOperationQueue.mainQueue()){(accelerometerData:CMACcelerometerData?, error:NSError?)in
        iflet accelerometerData=accelerometerData{
            myXLabel.text="x=\(accelerometerData.acceleration.x)"
            myYLabel.text="y=\(accelerometerData.acceleration.y)"
            myZLabel.text="z=\(accelerometerData.acceleration.z)"
        }
    }


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.