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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:26:49+00:00 2026-05-11T21:26:49+00:00

How to create & access static string in iPhone (objective c)? I declare static

  • 0

How to create & access static string in iPhone (objective c)?
I declare static NSString *str = @"OldValue" in class A.

If i assign some value to this in class B as str = @"NewValue".
This value persists for all methods in class B. But if I access it in class C (after assignment in B) I am getting it as OldValue.
Am I missing something? Should i use extern in other classes?

Thanks & Regards,
Yogini

  • 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-11T21:26:49+00:00Added an answer on May 11, 2026 at 9:26 pm

    Update: As of Xcode 8, Objective-C does have class properties. Note, it’s mostly syntactic sugar; these properties are not auto-synthesized, so the implementation is basically unchanged from before.

    // MyClass.h
    @interface MyClass : NSObject
    @property( class, copy ) NSString* str;
    @end
    
    // MyClass.m
    #import "MyClass.h"
    
    @implementation MyClass
    
    static NSString* str;
    
    + (NSString*) str 
    {
        return str;
    }
    
    + (void) setStr:(NSString*)newStr 
    {
        if( str != newStr ) {
            str = [newStr copy];
        }
    }
    @end
    
    
    // Client code
    MyClass.str = @"Some String";
    NSLog( @"%@", MyClass.str ); // "Some String"
    

    See WWDC 2016 What’s New in LLVM. The class property part starts at around the 5 minute mark.

    Original Answer:

    Objective-C doesn’t have class variables, which is what I think you’re looking for. You can kinda fake it with static variables, as you’re doing.

    I would recommend putting the static NSString in the implementation file of your class, and provide class methods to access/mutate it. Something like this:

    // MyClass.h
    @interface MyClass : NSObject {
    }
    + (NSString*)str;
    + (void)setStr:(NSString*)newStr;
    @end
    
    // MyClass.m
    #import "MyClass.h"
    
    static NSString* str;
    
    @implementation MyClass
    
    + (NSString*)str {
        return str;
    }
    
    + (void)setStr:(NSString*)newStr {
        if (str != newStr) {
            [str release];
            str = [newStr copy];
        }
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 196k
  • Answers 196k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer var bar = $("#bar", $(code)).text(); or var bar = $("#bar",… May 12, 2026 at 7:13 pm
  • Editorial Team
    Editorial Team added an answer Wouldn't you know, after about half an hour of mindless… May 12, 2026 at 7:13 pm
  • Editorial Team
    Editorial Team added an answer If you're using Apache, you can also embed PHP code… May 12, 2026 at 7:13 pm

Related Questions

I'm studying how to build java webapps for JBossAS 5.1.0 and I'm trying to
I'm trying to create a bookmarklet for posting del.icio.us bookmarks to a separate account.
I am attempting to load from text files, queries into an MS Access Queries
I have a .jar file I'm putting together. I want to create a really

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.