{"id":4628,"date":"2014-03-30T14:04:19","date_gmt":"2014-03-30T14:04:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/initializer-element-is-not-a-compile-time-constant-why-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:04:19","modified_gmt":"2014-03-30T14:04:19","slug":"initializer-element-is-not-a-compile-time-constant-why-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/initializer-element-is-not-a-compile-time-constant-why-collection-of-common-programming-errors\/","title":{"rendered":"&ldquo;Initializer element is not a compile-time constant&rdquo; why?-Collection of common programming errors"},"content":{"rendered":"<p>Because <code>[NSArray arrayWithArray: self.container.objects ]<\/code> isn&#8217;t a compile-time constant, it&#8217;s an expression that must be evaluated at runtime. In C and Objective-C, <code>static<\/code> variables inside functions must be initialized with <em>compile-time<\/em> constants, whereas C++ and Objective-C++ are more lenient and allow non-compile-time constants.<\/p>\n<p>Either compile your code as Objective-C++, or refactor it into something like this:<\/p>\n<pre><code>static NSArray *localArray = nil;\nif (localArray == nil)\n    localArray = [NSArray arrayWithArray: self.container.objects ];\n<\/code><\/pre>\n<p>Which is fairly similar to the code that the compiler would generate under the hood for a <code>static<\/code> variable initialized with a non-compile-time constant anyways (in actuality, it would use a second global flag indicating if the value was initialized, rather than using a sentinel value like <code>nil<\/code> here; in this case, we are assuming that <code>localArray<\/code> will never be <code>nil<\/code>). You can check out your compiler&#8217;s disassembly for that if you want.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because [NSArray arrayWithArray: self.container.objects ] isn&#8217;t a compile-time constant, it&#8217;s an expression that must be evaluated at runtime. In C and Objective-C, static variables inside functions must be initialized with compile-time constants, whereas C++ and Objective-C++ are more lenient and allow non-compile-time constants. Either compile your code as Objective-C++, or refactor it into something like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4628","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4628"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4628\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}