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 8647019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:57:40+00:00 2026-06-12T12:57:40+00:00

This might be a completely ridiculous question, but is it possible to use a

  • 0

This might be a completely ridiculous question, but is it possible to use a NSString as a substitute for a line of code?

for (int i = 0; i < 10: i++){    
    NSString *cam = @"locXCamProfileSwitch";
    ["%@", cam setOn:YES];
]

Also is it possible to concantinate the index i into the replacement of the X?

  • 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-12T12:57:42+00:00Added an answer on June 12, 2026 at 12:57 pm

    It’s not generally possible (as far as I know), but it’s possible to access ivars, properties, classes and methods by using strings.

    • Instance variables and properties can be accessed like this:

      [self valueForKey:@"key"];
      
    • Classes can be referenced like this:

      Class cls = NSClassFromString(@"MyClass");
      [cls aClassMethod];
      
    • Methods can be used like this:

      SEL selector = NSSelectorFromString(@"myMethod:");
      [self performSelector:selector];
      

    To replace a placeholder in a string with a number you can use a formatter:

    NSString *cam = [NSString stringWithFormat:@"loc%dCamProfileSwitch", i];
    

    That being said, it’s never a good idea to have numbered variable names.

    Use an array instead:

    int switchCount = 10;
    NSMutableArray *switches = [[NSMutableArray alloc] initWithCapacity:switchCount];
    for (int i = 0; i < switchCount; i++) {
        CGRect rect = CGRectMake(10, 10+i*30, 70, 40); // or something like that.
        UISwitch *sw = [[UISwitch alloc] initWithFrame:rect];
        sw.tag = i;
        [sw addTarget:self action:@selector(switchChanged:) 
                     forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:sw];
        [switches addObject:sw];
    }
    self.switches = [NSArray arrayWithArray:switches];  // assuming you have a property "switches".
    

    Then you can simply iterate over it:

    for (UISwitch *switch in self.switches) {
        [switch setOn:YES];
    }
    

    And be notified when one of them changes like this:

    - (void)switchChanged:(id)sender {
        UISwitch *theSwitch = (UISwitch *)sender; // the switch that changed.
        int tag = theSwitch.tag;  // number of switch that changed.
        // do something....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To a person skilled in programming, I know this question might seem ridiculous, but
This might be a very stupid question, but I still dont completely understand and
This might seem like a very easy question for some of you folks, but
This might be a simple question but I've searched and searched and can't find
This might be a very basic question but it confuses me. Can two different
This might sound like a stupid question but just trying to learn something here.
This might be a general mvp places and activities question, but the show case
This might be a weird question, but I'll try anyway. I set up my
This might be a loaded question, and it might get voted down, but it
I wonder if this might be a too subjective question for stackoverflow but ill

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.