4

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

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • 4
    Xcode 10 uses the new build system. Try changing it to Legacy Build System by going to `File -> Project/Workspace Settings -> Build System -> Legacy/Standard Build System`. – badhanganesh Jun 05 '18 at 13:52
  • I have updated my code and also got the answer ... basically AnyObject.frame was some how compiling properly in Xcode 9 not in Xcode 10 anymore – Mihir Mehta Jun 06 '18 at 06:18
  • @BadhanGanesh: This didn't mute the error, any help?! Mihir's answer won't help because it's a huge project and I can't apply his fix everywhere. – Tejas K Jan 17 '19 at 08:40
  • You have to apply fix i think. Because Apple will soon stop accepting build from Xcode 9. So you have to use XCode10 (Infact Xcode 10.1) and apply fix in case Legacy build system doesn't fix the issue – Mihir Mehta Jan 17 '19 at 13:13

2 Answers2

7

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

}
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
0

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;

} 
Krunal
  • 77,632
  • 48
  • 245
  • 261