"Cannot call value of non-function type 'CGVector'" error - swift3

I am currently trying to create a Flappy Bird clone on Swift 3. I have run into an error while finishing up with my physics for my "Cow". The error I am getting is "Cannot call value of non-function type 'CGVector'"
This is for a school project. I have already tried looking at top forums on the question and adding dx: and dy: before my numbers.
This is the line of code which is getting the error:
Cow.physicsBody?.velocity(CGVector(dx: 0, dy: 0))
This is my whole code for touchesBegan:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
Cow.physicsBody?.velocity(CGVector(dx: 0, dy: 0))
Cow.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 60))
}
I expect for CGVector to work. To summarize, I am using Swift 3 and am getting this error on my physics code for a Flappy Bird clone.

Please read the documentation:
Unlike function applyImpulse
func applyImpulse(_ impulse: CGVector)
velocity is a property
var velocity: CGVector { get set }
You have to assign the value
Cow.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
Or shorter
Cow.physicsBody?.velocity = CGVector.zero
Or shortest
Cow.physicsBody?.velocity = .zero

Related

iOS playing audio in iOS 13 throws C++ exception, freezes app

TL;DR:
The code below (all ten lines of it, apart from debugging)
fails (UI freezes) on iOS 13.x (Simulator)
succeeds (audio plays) on 14.x (Simulator and devices)
I don't have any devices with iOS 13.x. But...analytics from live apps suggest it is failing in the field on both iOS 13 and 14 devices. False positives? (See line of code commented with $$$.)
Steps To Reproduce
Create a new SwiftUI project that can run in iOS 13. Replace the text in ContentView.swift with the code below. Add an audio resource named clip.mp3. Build and run.
I am using Xcode 12.4, macOS 11.1, Swift 5.
See Also
Apple Dev Forum 1   // Unresolved
Apple Dev Forum 2   // Attributed to beta iOS/Xcode
Stackoverflow 1   // Unresolved
Stackoverflow 2   // Refers to next link
Apple Dev Forum 3   // Claims fixed in Xcode 12b5
[...and many more...]
Code
import SwiftUI
import AVKit
struct ContentView: View {
var body: some View {
Text("Boo!").onAppear { playClip() }
}
}
var clipDelegate: AudioTimerDelegate! // Hold onto it to forestall GC.
var player : AVAudioPlayer! // Ditto.
func playClip() {
let u = Bundle.main.url(forResource: "clip", withExtension: "mp3")!
player = try! AVAudioPlayer(contentsOf: u)
clipDelegate = AudioTimerDelegate() // Wait till now to instantiate, for correct timing.
player.delegate = clipDelegate
player.prepareToPlay()
NSLog("*** Starting clip play") // NSLog so we get timestamp.
player.play()
// Wait 5 seconds and see if audioPlayerDidFinishPlaying.
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
if let d = clipDelegate.clipDuration {
NSLog("*** Caller clip duration = \(d)")
} else {
NSLog("!!! Caller found nil clip duration")
// $$$ In live app, post audio-freeze event to analytics.
}
}
}
class AudioTimerDelegate: NSObject, AVAudioPlayerDelegate {
private var startTime : Double
var clipDuration: Double?
override init() {
self.startTime = CFAbsoluteTimeGetCurrent()
super.init()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
clipDuration = CFAbsoluteTimeGetCurrent() - startTime
NSLog("*** Delegate clip duration = \(clipDuration!)")
}
}
Console Output
Simulator iOS 14.4
The audio plays and the Console (edited for brevity) reads:
14:33:17 [plugin] AddInstanceForFactory: No factory registered for ... F8BB1C28-...
14:33:17 *** Starting clip play
14:33:19 *** Delegate clip duration = 1.692...
14:33:22 *** Caller clip duration = 1.692...
I gather that the first line is innocuous and related to the Simulator's sound drivers.
Is anyone else getting this console message with AVAudioPlayer in Xcode 11 (and 11.1)?
Device 14.4
Results are the same, without the AddInstanceForFactory complaint.
Simulator 13.6
Audio never sounds, the delegate callback never runs, and in the Console I get:
14:30:10 [plugin] AddInstanceForFactory: No factory registered for ... F8BB1C28-...
14:30:11 HALB_IOBufferManager_Client::GetIOBuffer: the stream index is out of range
14:30:11 HALB_IOBufferManager_Client::GetIOBuffer: the stream index is out of range
14:30:11 [aqme] AQME.h:254:IOProcFailure: AQDefaultDevice (1): output stream 0: null buffer
14:30:11 [aqme] AQMEIO_HAL.cpp:1774:IOProc: EXCEPTION thrown (-50): error != 0
14:30:26 [aqme] AQMEIO.cpp:179:AwaitIOCycle: timed out after 15.000s (0 1); suspension count=0 (IOSuspensions: )
14:30:26 CA_UISoundClient.cpp:241:StartPlaying_block_invoke: CA_UISoundClientBase::StartPlaying: AddRunningClient failed (status = -66681).
14:30:26 *** Starting clip play
14:30:26 HALB_IOBufferManager_Client::GetIOBuffer: the stream index is out of range
14:30:26 HALB_IOBufferManager_Client::GetIOBuffer: the stream index is out of range
14:30:26 [aqme] AQME.h:254:IOProcFailure: AQDefaultDevice (1): output stream 0: null buffer
14:30:26 [aqme] AQMEIO_HAL.cpp:1774:IOProc: EXCEPTION thrown (-50): error != 0
14:30:41 [aqme] AQMEIO.cpp:179:AwaitIOCycle: timed out after 15.000s (1 2); suspension count=0 (IOSuspensions: )
14:30:46 !!! Caller found nil clip duration
Remarks
It seems that there are two fifteen-second delays going on in the failure case.

This error keeps appearing in my code and I can't figure out how to fix it

I'm making an ios game app in Xcode 8 using swift 3 and I keep getting an error that says "Thread 1: EXC_BAD_INSTRUCTION (codeEXC_1386_INVOP, subcode=0x0)" and a console message that says fatal error: "Index out of range (lldb)". Does anyone know how to fix this?
Here's the section of code with the error. I get it on the line that says 'let nodeB = cableSegments[i]'
for i in 1..<length {
let nodeA = cableSegments[i - 1]
let nodeB = cableSegments[i]
let joint = SKPhysicsJointPin.joint(withBodyA: nodeA.physicsBody!, bodyB: nodeB.physicsBody!,
anchor: CGPoint(x: nodeA.frame.midX, y: nodeA.frame.minY))
scene.physicsWorld.add(joint)
}
The "out of range" error is common in many programming languages. It indicates to you that the loop is trying to access a position in the array that is out of the array range.
Per your code above it's not possible to figure out where are you obtaining the length value, but it should be the array length.
May the code below could work:
var counter = 0
for i in 0..<cableSegments.count {
counter += 1
if counter == cableSegments.count {
break
}
let nodeA = cableSegments[i]
let nodeB = cableSegments[i + 1]
let joint = SKPhysicsJointPin.joint(withBodyA: nodeA.physicsBody!, bodyB: nodeB.physicsBody!,
anchor: CGPoint(x: nodeA.frame.midX, y: nodeA.frame.minY))
scene.physicsWorld.add(joint)
}

Need Support for Apple Pencil/Finger drawing on Swift 3

I am making use of swift 2.3 with TouchCanvas (Apple Pencil Sample App),
in which while drawing,
I am able to switch between pen/pencil/brush/eraser - thickness & colors and the same is applied correspondingly.
Ref:
Now I upgraded to swift 3.0,
in which which drawing,
when switch between pen/pencil/brush/eraser - thickness & colors, the last picked one is applied to ALL.
Ref:
and also tried apple latest pencil API..Results were same
can please any one tell me exact solution for this..
Ahhhh...After trying long hours ...found an solution..
Just one line on CanvasView.swift
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()!
context.setLineCap(.round)
needsFullRedraw = false //Added this line
if (needsFullRedraw) {
setFrozenImageNeedsUpdate()
frozenContext.clear(bounds)
for array in [finishedLines,lines] {
for line in array {
line.drawCommitedPointsInContext(frozenContext, isDebuggingEnabled: isDebuggingEnabled, usePreciseLocation: usePreciseLocations)
}
}
needsFullRedraw = false
}
frozenImage = frozenImage ?? frozenContext.makeImage()
if let frozenImage = frozenImage {
context.draw(frozenImage, in: bounds)
}
for line in lines {
line.drawInContext(context, isDebuggingEnabled: isDebuggingEnabled, usePreciseLocation: usePreciseLocations)
}
}
or just commented the following line
/*if (needsFullRedraw) {
setFrozenImageNeedsUpdate()
frozenContext.clear(bounds)
for array in [finishedLines,lines] {
for line in array {
line.drawCommitedPointsInContext(frozenContext, isDebuggingEnabled: isDebuggingEnabled, usePreciseLocation: usePreciseLocations)
}
}
needsFullRedraw = false
}*/

selecting attitude reference frame in IOS 10 coremotion using swift 3

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"

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: