Search code examples
iosxcodeswiftcgsize

Cannot find an initializer for type 'CGSize' that accepts an argument list of type '(() -> _)' in Swift Project


I am working along with a Stanford course and am receiving this error:

Cannot find an initializer for type 'CGSize' that accepts an argument list of type '(() -> _)' 

This course was taught just a couple months ago but was using Xcode 6.2 instead of Xcode 6.3. What do I need to do differently to correct this error?

This is lecture 12 - Dynamic Animation - http://web.stanford.edu/class/cs193p/cgi-bin/drupal/

enter image description here

import UIKit

class DropItViewController: UIViewController {


    @IBOutlet weak var gameView: UIView!

    var dropsPerRow = 10

    var dropSize = CGSize {
        let size = gameView.bounds.size.width / CGFloat(dropsPerRow)
        return CGSize(width: size, height: size)
    }

    @IBAction func drop(sender: UITapGestureRecognizer) {
        drop()
    }

    func drop() {
        var frame = CGRect(origin: CGPointZero, size: dropSize)
        frame.origin.x = CGFloat.random(dropsPerRow) * dropSize.width

        let dropView = UIView(frame: frame)
        dropView.backgroundColor = UIColor.random

        gameView.addSubview(dropView)
     }
    }

    private extension CGFLoat {
    static func random(max: Int) -> CGFloat {
        return CGFloat(arc4random() % UInt32(max))

    }
    }

    private extension UIColor {
    class var random: UIColor {
        switch arc4random()%5 {
        case 0: return UIColor.greenColor()
        case 1: return UIColor.blueColor()
        case 2: return UIColor.orangeColor()
        case 3: return UIColor.redColor()
        case 4: return UIColor.purpleColor()
        default: return UIColor.blackColor()

    }
}

}

Solution

  • First of all, you typed CGFloat incorrectly. It is CGFloat not CGFLoat.

    For the second part, change '=' to ':' in dropSize:

    var dropSize : CGSize {
        let size = gameView.bounds.size.width / CGFloat(dropsPerRow)
        return CGSize(width: size, height: size)
    }
    

    Also, this is the first time I have seen '=' used instead of ':' in Swift, since its release...