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

  • Home
  • SEARCH
  • 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 362633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:15:56+00:00 2026-05-12T13:15:56+00:00

I was wanting to associate a set of rectangles with corresponding actions, so I

  • 0

I was wanting to associate a set of rectangles with corresponding actions, so I tried to do

struct menuActions {
    CGRect rect;
    SEL action;
};

struct menuActions someMenuRects[] = {
    { { { 0, 0 }, {320, 60 } }, @selector(doSomething) },
    { { { 0, 60}, {320, 50 } }, @selector(doSomethingElse) },
};

but I get the error “initializer element is not constant”. Is there some reason that what I’m trying to do isn’t allowed in general, or isn’t allowed at global scope, or do I have some kind of minor punctuation mistake?

  • 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-05-12T13:15:56+00:00Added an answer on May 12, 2026 at 1:15 pm

    This answer is to why "initializer element is not constant".

    Given the following example:

    SEL theSelector; // Global variable
    
    void func(void) {
      theSelector = @selector(constantSelector:test:);
    }
    

    Compiles to something like this for the i386 architecture:

      .objc_meth_var_names
    L_OBJC_METH_VAR_NAME_4:
      .ascii "constantSelector:test:\0"
    
      .objc_message_refs
      .align 2
    L_OBJC_SELECTOR_REFERENCES_5:
      .long   L_OBJC_METH_VAR_NAME_4
    

    This part defines two local (in terms of assembly code) ‘variables’ (actually labels), L_OBJC_METH_VAR_NAME_4 and L_OBJC_SELECTOR_REFERENCES_5. The text .objc_meth_var_names and .objc_message_refs, just before the ‘variable’ labels, tells the assembler which section of the object file to put “the stuff that follows”. The sections are meaningful to the linker. L_OBJC_SELECTOR_REFERENCES_5 is initially set to the address of L_OBJC_METH_VAR_NAME_4.

    At execution load time, before the program begins executing, the linker does something approximately like this:

    • Iterates over each entry in the .objc_message_refs
      section.
    • Each entry is initially set to a pointer to a 0 terminated C string.
    • In our example, the pointer is initially set to the
      address of L_OBJC_METH_VAR_NAME_4, which
      contains the ASCII C string
      "constantSelector:test:".
    • It then performs
      sel_registerName("constantSelector:test:")
      and stores the returned value at
      L_OBJC_SELECTOR_REFERENCES_5. The linker,
      which knows private implementation details,
      may not call sel_registerName() literally.

    Essentially the linker performs this at load time for our example:

    L_OBJC_SELECTOR_REFERENCES_5 = sel_registerName("constantSelector:test:");
    

    This is why the "initializer element is not constant"– the initializer element must be constant at compile time. The value is not actually known until the program begins executing. Even then, your struct declarations are stored in a different linker section, the .data section. The linker only knows how to update SEL values in the .objc_message_refs section, and there is no way to ‘copy’ that run-time calculated SEL value from .objc_message_refs to some arbitrary location in .data.

    The C source code…

    theSelector = @selector(constantSelector:test:);
    

    … becomes:

      movl    L_OBJC_SELECTOR_REFERENCES_5, %edx // The SEL value the linker placed there.
      movl    L_theSelector$non_lazy_ptr, %eax   // The address of theSelector.
      movl    %edx, (%eax)                       // theSelector = L_OBJC_SELECTOR_REFERENCES_5;
    

    Since the linker does all its work before the program is executing, L_OBJC_SELECTOR_REFERENCES_5 contains the exact same value you would get if you were to call sel_registerName("constantSelector:test:"):

    theSelector = sel_registerName("constantSelector:test:");
    

    The difference is this is a function call, and the function needs to do the actual work of finding the selector if its already been registered, or go through the process of allocating a new SEL value to register the selector. This is considerably slower that just loading a constant value. Though this is ‘slower’, it does allow you to pass an arbitrary C string. This can be useful if:

    • The selector is not known at compile time.
    • The selector is not known until just before sel_registerName() is called.
    • You need to vary the selector dynamically at run time.

    All selectors need to pass through sel_registerName(), which registers each SEL exactly once. This has the advantage of having exactly one value, everywhere, for any given selector. Though an implementation private detail, SEL is “usually” just a char * pointer to a copy of the selectors C string text.

    Now you know. And knowing is half the battle!

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

Sidebar

Related Questions

Wanting to implement authentication by client certificates I am experiencing some issues. First some
I wanting to show prices for my products in my online store. I'm currently
Not wanting to re-invent the wheel or anything, I was wondering if there's a
I'm wanting to parse a string into a nullable int in C#. ie. I
I'm wanting extra security for a particular point in my web app. So I
I am wanting to access a website from a different port than 80 or
I'm wanting to get the full value of a char[] variable in the VC6
I'm wanting to execute a program and as it runs read in it's output
I've been wanting to program for the Plan 9 operating system for a while.
I am wanting to find the distance between two different points. This I know

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.