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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:15:35+00:00 2026-06-08T07:15:35+00:00

So, I want to do something like: + (void) replace_text_with: (NSString *) const predefined_text_style;

  • 0

So, I want to do something like:

+ (void) replace_text_with: (NSString *) const predefined_text_style;

Where I’ve defined some text styles using constant strings in a Constants.h and Constants.m file elsewhere.

I want to require that the parameter passed as predefined_text_style is one of those constants, and if it’s impossible to specifically limit it to those constants, at least require that it be a constant of some kind. Is this possible?

  • 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-08T07:15:37+00:00Added an answer on June 8, 2026 at 7:15 am

    My knowledge in Objective-C is a bit rusty but I bet it is not possible to restrict the parameters to a subset of strings. What I would recommend is to use enums:

    typedef enum {
        FOOMy1stValue,
        FOOMy2ndValue,
        FOOMy3rdValue
    } FOOEnum;
    

    Eventually, retrieve the constant strings based on the value of the enum:

    NSString *FOOGetStringFromEnum(FOOEnum e) {
        static NSString *strings[] = {
            @"My first value",
            @"My second value",
            @"My third value"
        };
        return strings[e];
    }
    

    As in:

    void FOORestrictedInput(FOOEnum e) {
        NSLog(@"Restricted string: %@", FOOGetStringFromEnum(e));
    }
    

    This is not a too great solution because you can pass integers in place of the values of the enum, but at least the type of the parameter will document what you are expecting.

    Another possibility is to create a new class whose instances will store the string constants. Those instances will be passed as parameters instead of strings. In theory, one can still create new instances of this class in addition to the ones used as constants, but it is harder to do it by accident than when using strings.

    Anyway, I feel both solutions are unnecessary overhead. I would not worry about restricting the passable values too much (at least not in Objective-C – it could be far easier in some other languages). However, as I do not know much about your context, those are some possible solutions.

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

Sidebar

Related Questions

I want to do something like template <typename T> void foo(const T& t) {
Let's say I want to do something like this void my_printf(char *fmt,...) { char
I want to do something like the following: void handleClick(){ //do stuff } void
I want to achieve something like this in C# 3.5: public void Register<T>() :
Ideally I want a function something like the following (this example doesn't compile): void
I want to do something like this: void* ptr = some_function(&ptr); Is this legal
I would like to extract some text from an html file using Regex. I
I want something like the following: foo.should_receive(:bar).with(an_instance_of(Bla))..... Is this possible? Edit: According to the
I want something like the following but would like it to be reusable for
I want something like a canvas, but where i'd be able to manipulate pixels

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.