0

Are there any compiler macro's which can be used with Swift? Often enough, I use templates regularly which regularly need quite a few sections altering and since moving to swift, I've lose the love of #warning

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21

2 Answers2

1

You can do to use // MARK:

// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    if let backgroundImageChangedBlock = self.backgroundImageChangedBlock {
        backgroundImageChangedBlock(backgroundImage: image)
    }

    self.dismissViewControllerAnimated(true, completion: nil)
}

// MARK: - UINavigationControllerDelegate Methods
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .None)
}

enter image description here

Xcode not supports #warning, but you can to use script phrase:https://stackoverflow.com/a/26869489/907422:

Add a new Run Script to your target's build phases tab (project settings > build phases > '+' > new run script phase), and paste the following code in the empty box:

TAGS="WARNING"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"

enter image description here

Community
  • 1
  • 1
Bannings
  • 10,376
  • 7
  • 44
  • 54
  • Thanks, it's not what I was hoping to hear, but I guess after a day of searching, I was trying to avoid using scripts, but it seems the only way out other than a custom Xcode plugin... – Adrian Sluyters May 27 '15 at 14:14
  • @AdrianSluyters It is not a good solution, but it works.. Please tell me if you know a better way... – Bannings May 27 '15 at 14:16
0

There are no compiler macros or other preprocessor directives in Swift. You can set build configurations but I suspect this is not what you are looking for.

What you can do is to use documentation to mark code sections as needing changes. You can also write unit tests that will fail unless your template is correctly implemented.

Assertions might also be a good way to have templated code fail unless it is implemented properly.

  • I disagree as there are quite a few undocumented directives... I've just stumped across a few by disassembling swift, see this site (translated) for some others that people have come across: https://translate.google.com/translate?sl=auto&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=http%3A%2F%2Fandelf.github.io%2Fblog%2F2014%2F06%2F06%2Fswift-attributes%2F&edit-text=&act=url – Adrian Sluyters May 27 '15 at 14:06
  • @AdrianSluyters Yes, there are [undocumented internal functions](http://www.russbishop.net/swift-don-t-do-this) that you probably should avoid using. They may change without warning or have unintended side effects. –  May 27 '15 at 14:14
  • I purely use them for my internal templates, so I'm not too fussed about them changing. I'd prefer a list of "warnings" that a full blown "halt" at compile time (or ominous bugs due to unfulfilled requirements. – Adrian Sluyters May 27 '15 at 18:02