CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift
Asked Answered
Y

11

254

After converting code to latest swift 3.0 I am shown this error.

enter image description hereenter image description here

Also tell me solution for CGSize = CGSizeMake(0,0)

static var frameAtStartOfPan: CGRect = CGRectZero
static var startPointOfPan: CGPoint = CGPointZero

Which is also unavailable.

Yep answered 21/6, 2016 at 14:19 Comment(1)
But after converting my previous code to swift3 i got this issue, would like to know if there is any change in syntax in updated versionYep
J
507

CGRect Can be simply created using an instance of a CGPoint or CGSize, thats given below.

let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 100, height: 100))  

// Or

let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))

Or if we want to specify each value in CGFloat or Double or Int, we can use this method.

let rect = CGRect(x: 0, y: 0, width: 100, height: 100) // CGFloat, Double, Int

CGPoint Can be created like this.

 let point = CGPoint(x: 0,y :0) // CGFloat, Double, Int

CGSize Can be created like this.

let size = CGSize(width: 100, height: 100) // CGFloat, Double, Int

Also size and point with 0 as the values, it can be done like this.

let size = CGSize.zero // width = 0, height = 0
let point = CGPoint.zero // x = 0, y = 0, equal to CGPointZero
let rect = CGRect.zero // equal to CGRectZero

CGRectZero & CGPointZero replaced with CGRect.zero & CGPoint.zero in Swift 3.0.

Jennefer answered 21/6, 2016 at 15:8 Comment(7)
Can you also tell me solution for CGSize = CGSizeMake(0,0) which is not woking in swift 3Yep
@Yep Updated the question, please check.Jennefer
CGPointZero and CGRectZero is also not available any solution for that? Updated Question please update answerYep
@Yep Updated.Jennefer
@Yep I still wonder why didn't you accept anyone of the answer? You need more clarification on this simple matter?Jennefer
"CGRectZero is unavailable in Swift" when actually it's a syntax change to CGRect.zero is a joke! These guys need to work on their compile warnings big time... They rely on Stack Overflow to bail them out constantlyWatertight
In your first example, you can use shorthand like this: let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))Herlindaherm
D
51

Quick fix for CGRectMake , CGPointMake, CGSizeMake in Swift3 & iOS10

Add these extensions :

extension CGRect{
    init(_ x:CGFloat,_ y:CGFloat,_ width:CGFloat,_ height:CGFloat) {
        self.init(x:x,y:y,width:width,height:height)
    }

}
extension CGSize{
    init(_ width:CGFloat,_ height:CGFloat) {
        self.init(width:width,height:height)
    }
}
extension CGPoint{
    init(_ x:CGFloat,_ y:CGFloat) {
        self.init(x:x,y:y)
    }
}

Then go to "Find and Replace in Workspace" Find CGRectMake , CGPointMake, CGSizeMake and Replace them with CGRect , CGPoint, CGSize

These steps might save all the time as Xcode right now doesn't give us quick conversion from Swift 2+ to Swift 3

Der answered 29/9, 2016 at 11:9 Comment(2)
Brilliant solution!Ybarra
@Ybarra hope it reduces your hassle :)Der
D
28

In Swift 3, you can simply use CGPoint.zero or CGRect.zero in place of CGRectZero or CGPointZero.

However, in Swift 4, CGRect.zero and 'CGPoint.zero' will work

Deadpan answered 12/10, 2016 at 6:7 Comment(0)
C
13

If you want to use them as in swift 2, you can use these funcs:

For CGRectMake:

func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
    return CGRect(x: x, y: y, width: width, height: height)
}

For CGPointMake:

func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
    return CGPoint(x: x, y: y)
}

For CGSizeMake:

func CGSizeMake(_ width: CGFloat, _ height: CGFloat) -> CGSize {
    return CGSize(width: width, height: height)
}

Just put them outside any class and it should work. (Works for me at least)

Chong answered 17/8, 2017 at 17:56 Comment(0)
K
9

The structures as well as all other symbols in Core Graphics and other APIs have become cleaner and also include parameter names. https://developer.apple.com/reference/coregraphics#symbols

CGRect(x: Int, y: Int, width: Int, height: Int)
CGVector(dx: Int, dy: Int)
CGPoint(x: Double, y: Double)
CGSize(width: Int, height: Int)

CGPoint Example:

let newPoint = stackMid.convert(CGPoint(x: -10, y: 10), to: self)

CGVector Example:

self.physicsWorld.gravity = CGVector(dx: 0, dy: gravity)

CGSize Example:

hero.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 16, height: 18))

CGRect Example:

let back = SKShapeNode(rect: CGRect(x: 0-120, y: 1024-200-30, width: 240, height: 140), cornerRadius: 20)

Apple Blog

Swift syntax and API renaming changes in Swift 3 make the language feel more natural, and provide an even more Swift-y experience when calling Cocoa frameworks. Popular frameworks Core Graphics and Grand Central Dispatch have new, much cleaner interfaces in Swift.

Kroo answered 21/6, 2016 at 20:8 Comment(0)
D
6

Swift 3 Bonus: Why didn't anyone mention the short form?

CGRect(origin: .zero, size: size)

.zero instead of CGPoint.zero

When the type is defined, you can safely omit it.

Defensive answered 16/2, 2017 at 9:19 Comment(0)
B
5

Try the following:

var myToolBar = UIToolbar.init(frame: CGRect.init(x: 0, y: 0, width: 320, height: 44))

This is for the swift's latest version

Bourg answered 31/10, 2016 at 14:31 Comment(1)
like it (Y), this would be easier for auto completionNubbin
M
4

You can use this with replacement of CGRectZero

CGRect.zero
Milicent answered 15/10, 2018 at 8:27 Comment(0)
M
2

For CGSize

CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)
Margrettmarguerie answered 14/4, 2019 at 18:37 Comment(0)
B
2

Instead of CGSizeMake add CGSize it will work. CGSizeMake is used in objective c.

you don't want to use make with swift

Bosley answered 22/9, 2021 at 13:20 Comment(0)
M
1

In Swift 4 it works like below

collectionView.setContentOffset(CGPoint.zero, animated: true)
Mcneely answered 18/6, 2018 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.