首页 > 解决方案 > Swift conditional #if compilation fails in expression list

问题描述

If have a fairly simple case where Swift (v4.2 as of asking) will not respect an #if conditional compilation block if it appears within an expression list.

Example:

let params = [
  ("a", "1"),
  ("b", "2"),
#if FOO
  ("c", "3"),
#endif
  ("d", "4")]

This produces the error: Expected expression in container literal

This seems like a ridiculously common scenario and doesn't seem to violate anything in the Swift docs on Compiler Control. I can work around this specific example by declaring params as a var and appending the conditional parameter, but this is not possible in other situations like function calls.

Anyone have ideas for better ways to work around this limitation or thoughts on why the limitation exists in the first place?

标签: swiftcompiler-errors

解决方案


推荐阅读