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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:24:18+00:00 2026-06-14T22:24:18+00:00

in classA i have : classB *classBI=[[classB alloc]init]; bits=[classBI data]; //bits has a property

  • 0

in classA i have :

classB *classBI=[[classB alloc]init];
bits=[classBI data]; //bits has a property here in classA,it gets data from B/
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(38.f, 20.f, 244.f, 43.f)];
label.text = bits;

in classB i have :

    @property (nonatomic,retain) NSString *data; //in .h file
    data=@"no data"; //at the init method of classB .

    //then after a while when something is happen in classB , data is changed to :
    data=[NSString stringWithFormat:@"data:%f,%f,%f,%f,%f,%f",
         dataBits[0],dataBits[1],dataBits[2],dataBits[3],dataBits[4],dataBits[5]  ];

classA is the main scene (cocos2d but it does not matter). at the start i can really see in the label that in classA the word “no data”, but when data is changed in classB, i cant see that change in the label that is on screen. it stay with the word: “no data ”
why is that ?
why i lost data ?
if i NSLOG data in classB ,right after it changed in there,i can see that its not null,and it has the new value. something is wrong with the instance of B ,in A, that get this string.

  • 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-14T22:24:19+00:00Added an answer on June 14, 2026 at 10:24 pm

    When you set the label’s text to the string pointed to by bits, it is storing a reference to that string. Later when you change data, it is creating a new string which data is pointing to, but bits and the label both still have the original pointer which is looking at the original string. You want to update them whenever data is changed:

    There are several approaches to take in this situation, but one of the easiest would be to observe the data property of classBI for changes, and update both bits and the label whenever it changes:

    [classBI addObserver:self 
              forKeyPath:@"data" 
                 options:NSKeyValueObservingOptionNew 
                 context:NULL];
    

    Then, whenever data changes, this method will be called:

    // Note that you will need to get a reference to 'label' in order for this to work:
    - (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
        if ([keyPath isEqual:@"data"]) {
            bits = [change objectForKey:NSKeyValueChangeNewKey];
            label.text = bits;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ClassB inherited from ClassA . ClassA has a nested ClassC with
I have the following query; classB inherits from classA classC is friend of classB
I have 2 classes, ClassA and ClassB ClassA has one BOOL variable set to
I have set up 3 classes. ClassA has FK of ClassB and ClassC respectively.
I have a classA which has a vector< classB* > vObject . class ClassA{
I have a class hierarchy like this: ClassA inherits from NSObject ClassB inherits from
Lets say I have classA which contains classB and both are [Serializable]. I assumed,
I have three classes: ClassA , ClassB , ClassC . ClassC extends ClassB which
I have two classes, ClassA and ClassB , as well as a many to
My situation: ClassA may or may not have a parent of type ClassB .

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.