删除 println() 发布版本 iOS Swift [英] Remove println() for release version iOS Swift

查看:19
本文介绍了删除 println() 发布版本 iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不在调试版本中,我想全局忽略我的 Swift 代码中的所有 println() 调用.我找不到任何可靠的分步说明,希望得到指导.有没有办法在全局范围内做到这一点,还是我需要用 #IF DEBUG/#ENDIF 语句包围每个 println() ?

I would like to globally ignore all println() calls in my Swift code if I am not in a Debug build. I can't find any robust step by step instructions for this and would appreciate guidance. is there a way to do this globally, or do I need to surround every println() with #IF DEBUG/#ENDIF statements?

推荐答案

如前所述,我是一名学生,需要更清楚地定义一些事情以便跟进.经过大量研究,我需要遵循的顺序是:

As noted, i am a student and need things defined a little more clearly to follow along. After lots of research, the sequence I needed to follow is:

在 Xcode 项目窗口左侧的 File Navigator 顶部单击项目名称.这是包含项目名称、构建目标数量以及 iOS SDK 版本的行.

Click on the project name at the top of the File Navigator at the left of the Xcode project window. This is line that has the name of the project, how many build targets there are, and the iOS SDK version.

选择构建设置标签并向下滚动到靠近底部的Swift Compiler - Custom Flags"部分.点击其他标志旁边的向下箭头以展开该部分.

Choose the Build Settings tab and scroll down to the "Swift Compiler - Custom Flags" section near the bottom. Click the Down Arrow next to Other Flags to expand the section.

单击调试行以选择它.将鼠标光标放在线的右侧并双击.将出现一个列表视图.单击列表视图左下方的 + 按钮以添加值.文本字段将变为活动状态.

Click on the Debug line to select it. Place your mouse cursor over the right side of the line and double-click. A list view will appear. Click the + button at the lower left of the list view to add a value. A text field will become active.

在文本字段中,输入文本 -D DEBUG 并按 Return 提交该行.

In the text field, enter the text -D DEBUG and press Return to commit the line.

向您的项目添加一个新的 Swift 文件.您将要为该文件创建一个自定义类,因此请按照以下内容输入文本:

Add a new Swift file to your project. You are going to want to make a custom class for the file, so enter text along the lines of the following:

class Log {

  var intFor : Int

  init() {
    intFor = 42
   }

  func DLog(message: String, function: String = __FUNCTION__) {
    #if DEBUG
      println("(function): (message)")
    #endif
  }
}

我今天在让 Xcode 接受这个类时遇到了麻烦,所以 init 可能比必要的更重量级.

I was having trouble getting the class to be accepted by Xcode today, so the init may be a bit more heavyweight than necessary.

现在,您需要在您打算使用新自定义函数代替 println() 的任何类中引用您的自定义类.在每个适用类中将此作为属性添加:

Now you will need to reference your custom class in any class in which you intend to use the new custom function in place of println() Add this as a property in every applicable class:

   let logFor = Log()

现在您可以用 logFor.DLog() 替换 println() 的任何实例.输出还包括调用该行的函数的名称.

Now you can replace any instances of println() with logFor.DLog(). The output also includes the name of the function in which the line was called.

请注意,在类函数内部,除非我将该函数的副本作为该类中的类函数,否则我无法调用该函数,并且 println() 也更灵活一些输入,所以我不能在我的代码中的每个实例中使用它.

Note that inside class functions I couldn't call the function unless I made a copy of the function as a class function in that class, and println() is also a bit more flexible with the input, so I couldn't use this in every instance in my code.

这篇关于删除 println() 发布版本 iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆