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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:16:55+00:00 2026-05-28T11:16:55+00:00

Have mercy, newbie here. I have a NSString value that I construct on the

  • 0

Have mercy, newbie here.

I have a NSString value that I construct on the fly, which is the name of a UILabel instance. I want to send the label a message to update its text. But, the two data types don’t match. Here’s enough code (I think):

In header file:

IBOutlet UILabel *Clue1; // IBOutlet and IBAction are IDE flags
IBOutlet UILabel *Clue2; // IB = interface builder
IBOutlet UILabel *Clue3;

In implementation file:

- (IBAction) newPuzzle:(id)sender { // Clear all fields & get new clue

    [Clue1 setText:@""]; // Clear the fields
    [Clue2 setText:@""]; 
    [Clue3 setText:@""]; 

    // Send up a randomly chosen new clue

    NSArray *clues = [NSArray arrayWithObjects:@"222", @"333", nil];
    NSInteger randomIndex = arc4random()%[clues count];
    NSString *aClue = [clues objectAtIndex:randomIndex];

    // The clue will be split into component digits and each piece sent to a different label

    for (NSInteger charIdx = 0; charIdx < aClue.length; charIdx++) {
        NSString *cluePos = [NSString stringWithFormat:@"Clue%d", charIdx + 1];
        NSLog(@"%@", cluePos); // works
        [cluePos setText:@"test"]; // Xcode notes the type mismatch
    }
}

There are some similar questions on SO, but none are close enough for me to recognize that they apply to my case, at least as far as I can tell. Using the terminology of another language (R), I need to “coerce” the class of cluePos from NSString to UILabel. I’m on Xcode 4.2.1 and OSX 10.7.2.

TIA.

  • 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-28T11:16:56+00:00Added an answer on May 28, 2026 at 11:16 am

    You can’t coerce a string into a label because they are fundamentally different. The string doesn’t have any knowledge of your view controller class or it’s properties (some of which happen to be labels).

    You can however use the valueForKey: method to get a property of an object by name, where the name is specified as a string. So to get a property called Clue1 on my view controller I’d say:

    UILabel *label = [self valueForKey:@"Clue1"];
    

    Or in your case, this:

    NSString *cluePos = [NSString stringWithFormat:@"Clue%d", charIdx + 1];
    UILabel *label = [self valueForKey:cluePos];
    label.text = @"test";
    

    (I’m assuming ‘self’ in this case refers to the view controller, but you can call this on any object that has properties.)

    Another way to do this is to turn your string into a selector using NSSelectorFromString. That would look like this:

    SEL selector = NSSelectorFromString(@"Clue1");
    UILabel *label = [self performSelector:selector];
    

    For your purposes either solution works equally well, however the advantage of using the selector is that you can pass arguments to the method call (so you could call a method that returns an object, not just access a property or IBOutlet).

    Note that both of these methods will raise an exception if you try to access a property or call a method that doesn’t exist. You can test if the property exists before calling it by saying:

    SEL selector = NSSelectorFromString(@"Clue1");
    BOOL labelExists = [self respondsToSelector:selector];
    if (labelExists)
    {
        UILabel *label = [self performSelector:selector];
        label.text = @"test";
    }
    else
    {
        //do something else
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML form that submits to a PHP page which initiates a
I have a big php object that I want to serialize and store in
I have two tables with the same structure: id name 1 Merry 2 Mike
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have user submitted tags that can be any type of (valid) UTF-8 string.
I have (hacked) this regex this far which matches any words between hyphens and
This question is language independent. I have an application that handles requests in a
I have a custom framework that, following the advice in Apple's Framework Programming Guide
I have a method that is going to the DB so all our JDBC
I have a bunch of old classic ASP pages, many of which show database

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.