在C/C++/Objective-C中,您可以使用编译器预处理器定义宏.此外,您可以使用编译器预处理器包含/排除代码的某些部分.
#ifdef DEBUG
// Debug-only code
#endif
Run Code Online (Sandbox Code Playgroud)
Swift中有类似的解决方案吗?
在大型项目中使用哪个更好,为什么更好:
#if DEBUG
public void SetPrivateValue(int value)
{ ... }
#endif
Run Code Online (Sandbox Code Playgroud)
要么
[System.Diagnostics.Conditional("DEBUG")]
public void SetPrivateValue(int value)
{ ... }
Run Code Online (Sandbox Code Playgroud) 我正在制作处理敏感信用卡数据的应用程序.
如果我的代码在调试模式下运行,我想将此数据记录到控制台并进行一些文件转储.
但是在最终的appstore版本上(即它在发布模式下运行时)必须禁用所有这些(安全隐患)!
我会尽力回答我的问题; 所以问题就变成了"这个解决方案路径是正确的还是最好的方式?"
// add `IS_DEBUG=1` to your debug build preprocessor settings
#if( IS_DEBUG )
#define MYLog(args...) NSLog(args)
#else
#define MYLog(args...)
#endif
Run Code Online (Sandbox Code Playgroud) 我今天正在写我的第一个剃刀页面,无法弄清楚如何进入 #if debug #else #endif
如何在剃刀中输入预处理器?
您遇到的最糟糕的 现实世界宏/预处理器滥用是什么(请不要设想IOCCC答案*哈哈*)?
如果它真的很有趣,请添加一个简短的片段或故事.目标是教一些东西,而不是总是告诉人们"永远不要使用宏".
ps:之前我曾经使用过宏...但是当我有一个"真正的"解决方案时,我最终会摆脱它们(即使真正的解决方案是内联的,它也会变得类似于宏).
额外:举一个例子,宏实际上比非宏解决方案更好.
相关问题: C++宏什么时候有用?
如何让printf显示枚举类型变量的值?例如:
typedef enum {Linux, Apple, Windows} OS_type;
OS_type myOS = Linux;
Run Code Online (Sandbox Code Playgroud)
而我需要的是类似的东西
printenum(OS_type, "My OS is %s", myOS);
Run Code Online (Sandbox Code Playgroud)
必须显示字符串"Linux",而不是整数.
我想,首先我必须创建一个值索引的字符串数组.但我不知道这是否是最美妙的方式.有可能吗?
替代标题以帮助搜索
- Xcode找不到标题
- 在Xcode中缺少.h
- 找不到Xcode .h文件
- 未找到词法或预处理器问题文件
我正在开发一个来自Xcode 3的iOS应用程序项目.我现在已经转移到Xcode 4,我的项目构建了许多静态库.
这些静态库还声明了公共头文件,这些头文件由应用程序代码使用.在Xcode 3.x中,标题被复制(作为构建阶段)public headers directory
,然后在应用程序项目中public headers directory
被添加到headers search list
.
在Xcode 4下,构建目录被移动到~/Library/Developer/Xcode/DerivedData/my-project
.
问题是我如何在标题搜索设置中引用这个新位置?看起来:
public headers directory
是相对于DerivedData
目录,但是headers search
目录是相对于其他东西(可能是项目位置)我应该如何在Xcode 4中为iOS开发设置静态库目标,以确保在尝试编译为依赖项时,使用静态库的客户端可以使用头文件?
我正在使用一个开源库,它似乎有很多预处理指令来支持除C之外的许多语言.这样我就可以研究库正在做什么了我想看看我在预处理后编译的C代码,更像是我写的东西.
gcc(或Linux中常用的任何其他工具)可以读取这个库,但输出的C代码将预处理转换为任何东西并且人类也可以读取吗?
我想添加一些C#"仅调试"代码,只有在调试人员请求它时才会运行.在C++中,我曾经做过类似以下的事情:
void foo()
{
// ...
#ifdef DEBUG
static bool s_bDoDebugOnlyCode = false;
if (s_bDoDebugOnlyCode)
{
// Debug only code here gets executed when the person debugging
// manually sets the bool above to true. It then stays for the rest
// of the session until they set it to false.
}
#endif
// ...
}
Run Code Online (Sandbox Code Playgroud)
我不能在C#中完全相同,因为没有本地静态.
问题:在C#中实现这一目标的最佳方法是什么?
高度重复的代码通常是一件坏事,并且有一些设计模式可以帮助减少这种情况.然而,由于语言本身的限制,有时它是不可避免的.以下示例来自java.util.Arrays
:
/**
* Assigns the specified long value to each element of the specified
* range of the specified array of longs. The range to be filled
* extends from index <tt>fromIndex</tt>, inclusive, to index
* <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
* range to be filled is empty.)
*
* @param a the array to be filled
* @param fromIndex the index of the first element (inclusive) to be
* filled with the specified value
* @param toIndex …
Run Code Online (Sandbox Code Playgroud)