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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:50:44+00:00 2026-06-03T01:50:44+00:00

I want to class that should be initialized only once and returns some value

  • 0

I want to class that should be initialized only once and returns some value which was computed the first time. Is below the right approach ?

@property (nonatomic, retain) NSString *userAgent;

@implementation UserAgent
@synthesize userAgent = _userAgent;


+ (NSString *) userAgentString
{
    UserAgent *thisClass;
    if(self == nil)
    {
        thisClass = [[UserAgent alloc] init];
    }

    if (thisClass.userAgent == nil)
    {
        return @"not initialized";
    }

    return thisClass.userAgent;
}
  • 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-03T01:50:45+00:00Added an answer on June 3, 2026 at 1:50 am

    No.

    1. thisClass is a local variable. This means, the value will be reset (to garbage) everytime +userAgentString is called. At least make it static.

    2. self‘s meaning is not what you expect inside a class method. Do you mean thisClass?

    3. Even with the above fixes, the method isn’t thread-safe, which may or may not be okay.

    See Create singleton using GCD's dispatch_once in Objective C and Singleton in iOS 5? as examples to properly construct a singleton.

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

Sidebar

Related Questions

I want to implement a derived class that should also implement an interface, that
I want to create a class that initializes a timer which will be used
Where should I place code that should only run once (and not once per
I have some read-only data that I want to initialise and then re-initialise periodically
I want to define a class that supports __getitem__ , but does not allow
I have a class that I want to include in a GWT module. Unfortunately,
I want to have a template class that looks something like what I have
I want to use a custom class that could handle querying easily. Are there
Suppose I have a Python class that I want to add an extra property
I start with a basic class that I want to manipulate in a List

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.