3

I am working on submitting my first Swift app on app store and I was just curious on how to handle the print statements that I have in the app. For example

do {
    try managedObjectContext.save()
} catch let error as NSError {
    print(error)
}
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Nilesh M.
  • 165
  • 1
  • 11

1 Answers1

6

You don't want to leave the print staments in for final release. I don't think apple will reject an app with print statements but better to not have them for release.

What you can do is create a global print func (new swift file or above any class) so your whole project can access it.

func print(items: Any..., separator: String = " ", terminator: String = "\n") { 
    #if DEBUG
    Swift.print(items[0], separator:separator, terminator: terminator)   
    #endif
} 

and call your print statements like normal

print("Hello")

You can also ignore the DEBUG flag and simply comment out the print line for release

func print(items: Any..., separator: String = " ", terminator: String = "\n") { 
   //Swift.print(items[0], separator:separator, terminator: terminator)   
}
crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • I already have "-D" and "COCOAPODS" in the list, how do i add custom flaag named "-D DEBUG" ? – TomSawyer Jun 23 '16 at 05:28
  • I just updated my answer for some more detailed instructions, alternatively have a look at my github project. – crashoverride777 Jun 23 '16 at 10:34
  • How about overriding `print` function and quote it when i want to archive the app? I saw the answer by matt here http://stackoverflow.com/questions/26913799/remove-println-for-release-version-ios-swift?lq=1 – TomSawyer Jun 23 '16 at 16:08
  • Hey, not sure what you mean. I have read the link and one of the suggestion is basically what I am doing. I think it is the easiest – crashoverride777 Jun 24 '16 at 16:53
  • Read the matt answer, he said create a print method. `func print(items: Any..., separator: String = " ", terminator: String = "\n") { Swift.print(items[0], separator:separator, terminator: terminator) }` and i don't know comment it in the archive the app action will help? – TomSawyer Jun 25 '16 at 07:46
  • Yeah this is very cool. Its basically my way but without the struct. Thank you very much, I will update my answer to reflect this. I used to do this but it stopped working, now I know why. Thanks again – crashoverride777 Jun 25 '16 at 08:38