Mihir Mehta
Mihir Mehta

Reputation: 13833

Xcode 10 'frame' is only available on iOS 12.0 or newer compilation error iOS 12 beta

I am trying to compile my project in Xcode 10.0 beta (10L176w) (10L176w) ... I am getting the error 'frame' is only available on iOS 12.0 or newer

Here is my code

@IBAction func btnAddToCartAction(_ sender: AnyObject) {
    let btnCart:UIButton = sender as! UIButton
    let boundsCenter:CGPoint = btnCart.bounds.offsetBy(dx: sender.frame.size.width/2, dy: btnCart.frame.size.height/2).origin;

}

Which compiles fine in Xcode 9

Upvotes: 5

Views: 4120

Answers (2)

Krunal
Krunal

Reputation: 79646

Click on error (hint) and choose solution (Fix) of your query. (It will suggest you possible solution)

enter image description here

Or

Replace AnyObject with UIButton in function parameter argument type.

@IBAction func btnAddToCartAction(_ sender: UIButton) {
    //let btnCart:UIButton = sender as! UIButton
    let boundsCenter:CGPoint = sender.bounds.offsetBy(dx: sender.frame.size.width/2, dy: sender.frame.size.height/2).origin;

}

Or

Use btnCart instance, inplace of sender

@IBAction func btnAddToCartAction(_ sender: AnyObject) {
    let btnCart:UIButton = sender as! UIButton
    let boundsCenter:CGPoint = btnCart.bounds.offsetBy(dx: btnCart.frame.size.width/2, dy: btnCart.frame.size.height/2).origin;

} 

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13833

Basically in Xcode 9 AnyObject.frame was compiling successfully , but in XCode10 it stops compiling which make sense...

You need to convert it into UIButton or UIView before accessing it's frame property ...

So final code would be

@IBAction func didTapOnCheckMarkButton(_ sender: AnyObject) {

        let btnCart:UIButton = sender as! UIButton
        let boundsCenter:CGPoint = btnCart.bounds.offsetBy(dx: btnCart.frame.size.width/2, dy: btnCart.frame.size.height/2).origin;
         ...

}

Upvotes: 7

Related Questions