简体   繁体   中英

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

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;
         ...

}

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

在此输入图像描述

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;

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM