247

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.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
niravdesai21
  • 4,718
  • 3
  • 18
  • 32
  • 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 version – niravdesai21 Jun 21 '16 at 14:25

10 Answers10

501

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.

  • Can you also tell me solution for CGSize = CGSizeMake(0,0) which is not woking in swift 3 – niravdesai21 Jun 22 '16 at 05:40
  • @niravdesai21 Updated the question, please check. –  Jun 22 '16 at 05:59
  • CGPointZero and CGRectZero is also not available any solution for that? Updated Question please update answer – niravdesai21 Jun 22 '16 at 06:10
  • @niravdesai21 Updated. –  Jun 22 '16 at 07:36
  • @niravdesai21 I still wonder why didn't you accept anyone of the answer? You need more clarification on this simple matter? –  Jun 23 '16 at 06:16
  • 4
    "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 constantly – Magoo Oct 04 '16 at 09:34
  • In your first example, you can use shorthand like this: `let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))` – Code Different Feb 22 '17 at 21:10
49

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

Rafat touqir Rafsun
  • 2,567
  • 22
  • 23
27

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

guru
  • 2,201
  • 21
  • 31
12

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)

reformed
  • 3,922
  • 9
  • 51
  • 77
CaOs433
  • 785
  • 1
  • 8
  • 13
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.

Edison
  • 10,891
  • 4
  • 36
  • 46
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

Evgeniy Mishustin
  • 2,858
  • 2
  • 31
  • 68
BurakLGN
  • 51
  • 1
  • 3
5

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.

Dmitry Isaev
  • 3,463
  • 2
  • 32
  • 47
2

You can use this with replacement of CGRectZero

CGRect.zero
jaskiratjd
  • 679
  • 1
  • 6
  • 16
0

In Swift 4 it works like below

collectionView.setContentOffset(CGPoint.zero, animated: true)
Neha
  • 618
  • 1
  • 9
  • 15
0

For CGSize

CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)
Team chang
  • 173
  • 5