selecting attitude reference frame in IOS 10 coremotion using swift 3 - swift3

I am trying to use CMMotionManager to update the attitude of a camera viewpoint in scenekit. I am able to get the following code using the default reference to work.
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdates(to: motionQueue, withHandler:{ deviceManager, error in
if (deviceManager?.attitude) != nil {
let rotation = deviceManager?.attitude.quaternion
OperationQueue.main.addOperation {
self.cameraNode.rotation = SCNVector4(rotation!.x,rotation!.y,rotation!.z,rotation!.w)
}
}
})
I am however unable to get startDeviceMotionUpdates to work with a selected reference frame as shown below:
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrameXMagneticNorthZVertical, to: motionQueue, withHandler:{ deviceManager, error in
if (deviceManager?.attitude) != nil {
let rotation = deviceManager?.attitude.quaternion
OperationQueue.main.addOperation {
self.cameraNode.rotation = SCNVector4(rotation!.x,rotation!.y,rotation!.z,rotation!.w)
}
}
})
The error i receive is:
Use of unresolved identifier 'CMAttitudeReferenceFrameXMagneticNorthZVertical'
I get similar error messages for other reference frames as well. Can anyone shed any light on the use of the "using:" parameter for the startDeviceMotionUpdates function? All the examples i have found are for older versions of swift or objective c so it is quite possible that it is simply an issue with not understanding Swift 3 syntax.

After some additional fiddling i figured out that the using argument expects a member of the new CMAttitudeReferenceFrame struct. i.e. it should be passed as:
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xMagneticNorthZVertical
,to: motionQueue, withHandler:{
deviceManager, error in
if (deviceManager?.attitude) != nil {
let rotation = deviceManager?.attitude.quaternion
OperationQueue.main.addOperation {
self.cameraNode.rotation = SCNVector4(rotation!.x,rotation!.y,rotation!.z,rotation!.w)
}
}
})
This is a change from earlier version that allowed the direct use of constants such as "CMAttitudeReferenceFrameXMagneticNorthZVertical"

Related

How to make an If statement check for if a variable equals a negative number; issue making an adventure game with X,Y coordinates

I'm new to Javascript (and coding in general), and I'm trying to make a pretty basic in-browser adventure game, but immediately I've encountered a problem. I wanted it to plot your location as if on an X,Y graph so I could easily mark out a map for it later with coordinates. So, you start at 0,0, and when you move West it changes to -1,0, so on, and I'm using If statements to check your location. But, it always assumes you're at the lowest negative possible, and so always says you're at 'Room 3'.
I think it does this because it doesn't recognise the - in the check as part of the number, and instead thinks you're asking if it equals the number minus itself, which would be zero, which is the game's default position. But, at the same time, it still thinks you're at the lowest possible position even if you move off 0,0, so I don't know. If that is the case, I would like to know how to fix it, and if that isn't the case, I would like to know what the real problem is.
Below is the entire program so far. This is my first time really trying to code something by myself, with no tutorial involved, so I apologise for how training-wheels-y it is.
let x=0
let y=0
let time=0
let button = document.getElementById('button');
let buttonNorth = document.getElementById('buttonNorth');
let buttonWest = document.getElementById('buttonWest');
let buttonEast = document.getElementById('buttonEast');
let buttonSouth = document.getElementById('buttonSouth');
button.onclick = function() {
reporter()
locatron()
}
buttonNorth.onclick = function() {
updateNorth()
reporter()
locatron()
}
buttonWest.onclick = function() {
updateWest()
reporter()
locatron()
}
buttonEast.onclick = function() {
updateEast()
reporter()
locatron()
}
buttonSouth.onclick = function() {
updateSouth()
reporter()
locatron()
}
function reporter() {
console.log(`location is: ${x},${y}`)
console.log(`time is: ${time}`)
}
function updateNorth() {
y = y+1
time=time+1
}
function updateWest() {
x = x-1
time=time+1
}
function updateEast() {
x = x+1
time=time+1
}
function updateSouth() {
y = y-1
time=time+1
}
function locatron() {
if(x==0,y==0) {
totalDescription.innerHTML = `Room 1`
}
if(x==-1,y==0) {
totalDescription.innerHTML = `Room 2`
}
if(x==-2,y==0) {
totalDescription.innerHTML = `Room 3`
}
else {
totalDescription.innerHTML = `This area has not been written yet.`
}
}
I've tried turning the number in the If statement into a string, but it just does the same thing. The only way I can get it to display any message other than 'Room 3' is by moving it along the Y axis, at which point it changes to my equivalent of an error message, 'This area has not been written yet.'. I also tried looking the problem up, but the wording on it is so finnicky that the only results I would get back are how to 'check if a variable is negative', which is not at all what I want.
I appreciate any help I can get.

Swift 2 to 3 conversion error with Value of type '[Any]' has no member

I'm having a problem with a Swift 2 to 3 conversion piece of work and some of the remains syntax giving: Value of type '[Any]' has no member errors.
I was hoping someone could point me at a good solution.
Swift 2 code
Swift 2 code
func search() {
epsonPrinters = [Printer]()
starPrinters = [Printer]()
epson_startSearching()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { [unowned self] in
let devices = SMPort.searchPrinter()
self.starPrinters = devices.map { portInfo -> Printer in
let p = Printer(
id: portInfo.modelName,
make: "Star Micronics",
model: portInfo.modelName,
portName: portInfo.portName)
if let name = portInfo.modelName as? String {
p.emulation = name.containsString("TSP143") ? "StarGraphics" : "StarLine"
}
return p
}
}
}
Swift 3 Code (I've added comments above areas with errors)
func search() {
epsonPrinters = [Printer]()
starPrinters = [Printer]()
epson_startSearching()
DispatchQueue.global(qos: .background).async { [unowned self] in
let devices = SMPort.searchPrinter()
self.starPrinters = [devices.map { portInfo -> Printer in
// id, model and portName in below fails with messages like:
// Value of type '[Any]' has no member 'modelName'
let p = Printer(
id: portInfo.modelName,
make: "Star Micronics",
model: portInfo.modelName,
portName: portInfo.portName)
// error on portInfo.modelName
// Value of type '[Any]' has no member 'modelName'
if let name = portInfo.modelName as? String {
p.emulation = name.containsString("TSP143") ? "StarGraphics" : "StarLine"
}
return p
}!]
}
}
I know that I can replace the 'id:...' part with the likes of:
id: ((portInfo[0] as AnyObject).modelName) ?? "",
But this isn't correct because PortInfo can have none, 1 or multiples depending on the number of printers we find.
I'd appreciate any suggestions for refactoring this in an elegant way that is good Swift 3 syntax and likely to survive into Swift 4.
I'm working in Xcode 8.3.2
When you get some errors about types, you'd better check what type each variable has. When you select devices in the line let devices = ..., Quick Help of Xcode will show you something like this:
Declaration let devices: [Any]?
First, it's an Optional and you need to unwrap it, before using the actual content.
Second, the type of the elements in devices is Any, to which you cannot apply any methods (including property accessors). You need to cast it to an appropriate type at an appropriate place.
To solve the two things above, you can write something like this:
guard let devices = SMPort.searchPrinter() as? [PortInfo] else {
fatalError("This may never happen")
}
With guard statement above, Quick Help will show you:
Declaration let devices: [PortInfo]
Non-Optional, simple Array of PortInfo, so you can use any methods of PortInfo for the elements of this devices.
I would translate your Swift 2 code into Swift 3 like this:
func search() {
epsonPrinters = []
starPrinters = []
epson_startSearching()
DispatchQueue.global(qos: .default).async {
guard let devices = SMPort.searchPrinter() as? [PortInfo] else {
fatalError("This may never happen")
}
self.starPrinters = devices.map { portInfo -> Printer in
let p = Printer(
id: portInfo.modelName,
make: "Star Micronics",
model: portInfo.modelName,
portName: portInfo.portName)
if let name = portInfo.modelName {
p.emulation = name.contains("TSP143") ? "StarGraphics" : "StarLine"
}
return p
}
}
}
You may need some fixes (not many, I believe) to use this code, as you are not showing all relevant things such as the definition of Printer.

"Ambiguous use of prefix" compiler error with Swift 3

I just migrated my project from Swift 2.2 to Swift 3.0 with Xcode 8 beta.
I have something similar to the following code (you can paste this into a playground):
import Foundation
let datesWithCount: [(Date, Int)] = [(Date(), 1), (Date(), 2), (Date(), 3)]
let dates: [Date] = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1).map {
return $0.0
}
In Swift 2.2 this compiled fine. However, with Swift 3.0 I get the error
Ambiguous use of 'prefix'
The only way to get this to compile in Swift 3.0 is to split out the map into a separate line:
let sortedDatesWithCount = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)
let mappedDates = sortedDatesWithCount.map {
return $0.0
}
BTW, in the actual code I'm returning NSNotification objects from the map not Dates but the error is the same. I just used Date here for making the example simple.
Is there any way to get this to compile as a one liner?
UPDATE: Created a JIRA for the Swift project.
It works if you make the ArraySlice into an Array before passing it to map:
let dates: [Date] = Array(datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)).map { return $0.0 }
This looks like a type inference bug in the compiler.

iOS 8 prepare for segue Casting object. Gives Error

So my code below shows me how I am doing prepare for segue. I'm not sure whats going on but its giving me some weird error I've used this same way before and it works but it just won't work in this application.
Code
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if segue.identifier == "select"
{
println("helloworld")
var index = tableView.indexPathForCell(sender as UITableViewCell!)
var object = self.objects[index!.row] as HMAccessory // object is a NSmutableArray its crashing on this next line where im casting it
//var dest = segue!.destinationViewController? as CharacristicsViewController!
// dest.detailItem = object
}
}
can anybody see anything wrong with this?
This is the error I'm getting.
HomeKit`HomeKit.AccessoryTableViewController.prepareForSegue
(HomeKit.AccessoryTableViewController)(Swift.ImplicitlyUnwrappedOptional,
sender : Swift.ImplicitlyUnwrappedOptional) -> () at
AccessoryTableViewController.swift:23:

Odd ColdFusion Behavior--Abort Not Honored

Using ColdFusion 9.01, occasionally, we have observed an issue where an error may be occurring within a CFC function and when we attempt to add writeDump(foo); and abort; calls to debug the error ColdFusion does not honor those calls.
Example:
private void function index(Event)
{
var rc = Event.getCollection();
var prc = Event.getCollection(private=true);
/** NOT HONORED! **/
writeDump(var=rc);
abort;
prc.JSON = {};
prc.JSON.show = variables.APIProxy.call(
handler = 'shows'
,action = 'read'
,event = arguments.Event
/** THE ERROR IS OCCURING HERE **/
,params = { language=lcase(rc.language.getLanguage_Medium()), show=rc.show_name }
);
prc.JSON.showEpisodes = variables.APIProxy.call(
handler = 'episodes'
,action = 'index'
,event = arguments.Event
,params = { language=lcase(rc.language.getLanguage_Medium()), show=rc.show_name, detail=true }
);
prc.JSON.products = variables.APIProxy.call(
handler = 'products'
,action = 'index'
,event = arguments.Event
,params = { language=lcase(rc.language.getLanguage_Medium()), detail=true }
);
Event.addAssets(
'model/product.js
,model/show.js
,collection/product_mobile.js
,collection/show_mobile.js
,view/product_mobile.js
,view/productList.js
,view/show_mobile.js
,view/showList.js
,model/episode.js
,view/episode_mobile.js
,view/episodeList.js
,collection/episode_mobile.js
,collection/product_mobile.js
,mobile/episodeObject.css
,mobile/show.js
,mobile/show.css
,mobile/category.css
');
Event.setLayout('layout.mobile');
Event.setView("show/index_mobile");
return;
}
I believe we have successfully eliminated caching. I am curious if anyone else has encountered this.
Thank you.
Aaron
I'm guessing that the error is a parse error, not a true runtime error, so it gets thrown before the function actually executes. It's not actually skipping over your abort, it just fails to parse (or execute) the entire thing.
I'm not sure why you're getting a parse error there, but I do know the CF code that handles struct literals is somewhat flaky.
The issue was with the struct literals declared within the argument calls to a function.
i'm going to go out on a limb here and say that your issue might have something to do with this bug:
http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html#bugId=86960
is there anything in your app that executes in the onRequestEnd() method?
it would be helpful to tell us what exactly is happening and/or the output you're getting when the issue happens.