Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:14:45+00:00 2026-06-15T22:14:45+00:00

I’m declaring: static NSString *a = @a; and this is a correct declaration in

  • 0

I’m declaring:

static NSString *a = @"a";

and this is a correct declaration in iOS6 (it should be more correct to use the compiler version but I do not know it at the moment). I thought that with number literals also:

static NSNumber *b=@1;

could be a valid declaration. Compiler tells me that initializer element is not a compile time constant. This surprises me a bit. Since NSNumber is immutable as NSString and since I’m using a literal as in string case, I thought it could be valid as well.

Does anybody have a reasonable explanation about this difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:14:47+00:00Added an answer on June 15, 2026 at 10:14 pm

    The first line is a compile-time constant since you are assigning @"a" and not something like static NSString *a = [NSString stringWithFormat:@"a"];(That will throw the same error)

    But for an NSNumber, static NSNumber *b = @1; is actually equivalent to static NSNumber *b = [NSNumber numberWithInt:1];. For more details check Objective-C Literals.

    Note that in the above case, the right side is not a compile-time constant. It is an expression that must be evaluated at runtime. In C and Objective-C, static variables must be initialized with compile-time constants.

    If you want to have NSNumber as a const you can check with the approaches mentioned here Objective C – How to use extern variables?.

    Also check this on Objective C literals from Mike Ash,

    It’s important to note that none of the new literal syntax qualifies
    as a compile-time constant.

    And,

    NSString literals are also compile-time constants, because of a tight
    coupling between the compiler and the libraries. There’s a special
    NSString subclass called NSConstantString with a fixed ivar layout:

    This tight coupling has advantages, like producing legal global
    variable initializers, and requiring no extra code to run to build the
    object at runtime. However, there are big disadvantages as well. The
    NSConstantString layout is set forever. That class must be maintained
    with exactly that data layout, because that data layout is baked into
    thousands of third-party apps. If Apple changed the layout, those
    third-party apps would break, because they contain NSConstantString
    objects with the old layout.

    If NSArray literals were compile-time constants, there would need to
    be a similar NSConstantArray class with a fixed layout that the
    compiler could generate, and that would have to be maintained
    separately from other NSArray implementations. Such code could not run
    on older OSes which didn’t have this NSConstantArray class. The same
    problem exists for the other classes that the new literals can
    produce.

    This is particularly interesting in the case of NSNumber literals.
    Lion introduced tagged pointers, which allow an NSNumber’s contents to
    be embedded directly in the pointer, eliminating the need for a
    separate dynamically-allocated object. If the compiler emitted tagged
    pointers, their format could never change, and compatibility with old
    OS releases would be lost. If the compiler emitted constant NSNumber
    objects, then NSNumber literals would be substantially different from
    other NSNumbers, with a possible significant performance hit.

    Instead, the compiler simply emits calls into the framework,
    constructing the objects exactly like you would have done manually.
    This results in a bit of a runtime hit, but no worse than building
    them yourself without the new syntax, and makes for a much cleaner
    design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.