Missing Arguments for parameters swift [duplicate] - swift

Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)

Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.

I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)

Related

Spritekit - could not create physics body - image occasionally returns nil

I have looked at these questions but their answers did not work for me: 1, 2. I am targeting iOS 10.0. Occasionally when running the code a physicsBody is not created due to a problem with the texture/image and occasionally it does get created but is completely wrong yet I can't find anything wrong with the image, no stray pixels. Sometimes it does get created perfectly fine, about 1/3 times. Setting the alpha threshold as one answer suggests also doesn't change anything.
Code:
let pigeon: SKSpriteNode = SKSpriteNode(imageNamed: "pigeonHitBox")
init(){
pigeon.size = CGSize(width: 100, height: 100)
pigeon.position = CGPoint(x: 400, y: 631)
pigeon.physicsBody = SKPhysicsBody(texture: pigeon.texture ?? SKTexture(imageNamed: "pigeonHitBox"), size: pigeon.size)
pigeon.physicsBody!.isDynamic = true
}
Example of completely wrong outline

Creating a node att random CGPoint on screen

I want to create a shape at a random position on a screen in a universal app.
If I make a GKRandomDistribution the shape.position.x or shape.position.y gives me an error saying that it cannot convert an Int into a CGPoint.
If I knew the size of the screen this would be easy, but since I want to make this universal all I have to go on is self.frame and I can't find a way of saying something like GKRandomDistribution(lowestValue: <min value of screen>, highestValue: <max value of screen>) for my positions.
I'd love some help with this!
I'm assuming you'll be calling this within one of your SKScene subclasses. If I understand your question correctly, this should do the trick
func randomPosition() -> CGPoint {
let x = CGFloat.random(in: 0...frame.maxX)
let y = CGFloat.random(in: 0...frame.maxY)
return CGPoint(x: x, y: y)
}

Nil is not compatible with expected argument [duplicate]

Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)
Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.
I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)

Why is 'nil' not compatible with 'UnsafePointer<CGAffineTransform>' in Swift 3?

Trying to create a CGMutablePath() as such:
let path = CGMutablePath()
CGPathMoveToPoint(path, nil, 30, 0)
but the compiler keeps on giving me the following error: 'nil' not compatible with expected argument type 'UnsafePointer<CGAffineTransform>'. What am I doing wrong? I can't seem to find anything else online.
(Yes I've tried converting the Ints to CGFloats but it doesn't seem to make a difference.)
Try this:
let path = CGMutablePath()
path.move(to: CGPoint(x: 30, y: 0))
CGPath APIs are now imported as instance methods in Swift 3.
You can check them with Command-clicking on CGMutablePath.
Or see the latest reference of CGMutablePath.
I was getting this error when trying to draw inside a CGRect
This solved it for me:
let framePath : CGMutablePath = CGMutablePath()
framePath.addRect(frameRect)
I hope this saves you countless hours trying to solve this. :)

In Xcode 6.1. 'UIImage?' does not have a member named 'size' Error

I'm getting an image's Size and used the below code. It was working perfectly fine with Xcode 6.0.1. After updating to Xcode 6.1, I'm getting Error Like :
'UIImage?' does not have a member named 'size'
var image = UIImage(named: imageName)
let sizeOfImage = image.size
Is it bug with my code or Apple ? Please help me in here. TIA.
That initializer is now a failable initializer, and as such it returns an optional UIImage.
To quickly fix your bug just unwrap the image:
let sizeOfImage = image?.size
but I presume you will reference the image variable more than just once in your code, in that case I suggest using optional binding:
if let image = image {
let sizeOfImage = image.size
/// ...
/// Use the image
}
In addition to #Antonio's answer you can use nil coalescing operator(??) to get a default size in case image initializer is failed. Here is how:
let size = image?.size ?? CGSizeZero
Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. In your case: image.size!
you can use like this:
let image = UIImage(named: "photo1.png")
let sizeOfImage = image?.size
imageView.image = image
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: sizeOfImage!)