1

I have just started to shift from Obj-C to Swift in Xcode.

Is there no way to use macros from #define any more? I understand that I can use let to define constants instead, but I am used to having this set of macros at the top of most of my programs, for example:

    #define frameMinX CGRectGetMinX(self.frame)
    #define frameMinY CGRectGetMinY(self.frame)
    #define frameMidX CGRectGetMidX(self.frame)
    #define frameMidY CGRectGetMidY(self.frame)
    #define frameMaxX CGRectGetMaxX(self.frame)
    #define frameMaxY CGRectGetMaxY(self.frame)

    #define frameMin CGPointMake(frameMinX, frameMinY)
    #define frameMid CGPointMake(frameMidX, frameMidY)
    #define frameMax CGPointMake(frameMaxX, frameMaxY)
Shuri2060
  • 729
  • 6
  • 21
  • 4
    One question per post, please. – jscs Jul 09 '16 at 21:18
  • Ok - sorry about that. I'll start 2 new posts then – Shuri2060 Jul 10 '16 at 10:59
  • http://stackoverflow.com/questions/38291232/why-are-the-init-methods-inherited-from-a-class-not-suggested and http://stackoverflow.com/questions/38291203/how-do-swift-files-keep-the-privacy-which-the-h-and-m-files-did-in-obj-c now. – Shuri2060 Jul 10 '16 at 11:09
  • This may help people who are having the similar problems with shifting languages: http://stackoverflow.com/questions/24003291/ifdef-replacement-in-swift-language and http://stackoverflow.com/questions/24111854/in-absence-of-preprocessor-macros-is-there-a-way-to-define-practical-scheme-spe/24112024#24112024. – Shuri2060 Jul 11 '16 at 10:11
  • Don't use macros, you are just setting yourself up for debugging pain later. Just write functions, at least you can step through them and debug them. – MoDJ Jul 11 '16 at 20:45
  • I'm afraid macros do have their uses sometimes which I miss a lot... for example, optional compiling for different platforms (iOS/OSX), or for different versions.. – Shuri2060 Jul 11 '16 at 21:18

1 Answers1

3

You cannot use the #defines in Swift, but these all seem to be computations on the frame of a UIView, so you can extend UIView to add them as computed properties:

extension UIView {
    var frameMinX: CGFloat { return CGRectGetMinX(self.frame) }
    var frameMinY: CGFloat { return CGRectGetMinY(self.frame) }
    var frameMidX: CGFloat { return CGRectGetMidX(self.frame) }
    var frameMidY: CGFloat { return CGRectGetMidY(self.frame) }
    var frameMaxX: CGFloat { return CGRectGetMaxX(self.frame) }
    var frameMaxY: CGFloat { return CGRectGetMaxY(self.frame) }
    var frameMin: CGPoint  { return CGPointMake(frameMinX, frameMinY) }
    var frameMid: CGPoint  { return CGPointMake(frameMidX, frameMidY) }
    var frameMax: CGPoint  { return CGPointMake(frameMaxX, frameMaxY) }
}

Usage Example:

Inside of any subclass of UIView, you can use the properties just as you would the macros:

class CustomView: UIView {
    func demoProperties() {
        print(frameMinX)
        print(frameMax)
    }
}

let view = CustomView(frame: CGRect(x: 50, y: 100, width: 200, height: 300))

view.demoProperties()

Output:

50.0
(250.0, 400.0)
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Thanks - that works for me! I also forgot you can now set functions as variables which is very useful (another way). – Shuri2060 Jul 10 '16 at 10:58