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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:38:13+00:00 2026-05-14T04:38:13+00:00

I simply want to change a variable of an object from another class. I

  • 0

I simply want to change a variable of an object from another class. I can compile without a problem, but my variable always is set to ‘null’.
I used the following code:

Object.h:

@interface Object : NSObject {
    //...
    NSString *color;
    //...
}

@property(nonatomic, retain) NSString* color;

+ (id)Object;
- (void)setColor:(NSString*)col;
- (NSString*)getColor;
@end

Object.m:

+(id)Object{
    return [[[Object alloc] init] autorelease];
}

- (void)setColor:(NSString*)col {
    self.color = col;
}

- (NSString*)getColor {
    return self.color;
}

MyViewController.h

#import "Object.h"

@interface ClassesTestViewController : UIViewController {
    Object *myObject;
    UILabel *label1;
}

@property UILabel *label1;
@property (assign) Object *myObject;
@end

MyViewController.m:

#import "Object.h"
@implementation MyViewController
@synthesize myObject;

- (void)viewDidLoad {
    [myObject setColor:@"red"];
    NSLog(@"Color = %@", [myObject getColor]);
    [super viewDidLoad];
}

The NSLog message is always Color = (null)

I tried many different ways to solve this problem, but no success.
Any help would be appreciated.


Thanks for the help so far.

I modified the code as follow, but it still doesn’t work as it should.

MyViewController.h:
    #import <UIKit/UIKit.h>
    #import "Object.h"

    @interface MyViewController : UIViewController {
        Object *myObject;
    }
    @property (nonatomic, retain) Object *myObject;
    @end

MyViewController.m:

#import "MyViewController.h"
#import "Object.h"

@implementation MyViewController
@synthesize myObject;

- (void)viewDidLoad {
Object *myObject = [Object new];
myObject = 0;
[myObject setColor:@"red"];
NSLog(@"color = %@", myObject.color);
[super viewDidLoad];
}

If I do it like this, NSLog returns color = null (and I think myObject is only visible in viewDidLoad). How can declare myObject and make it visible in MyViewController?
I stripped down my Object class to

Object.h:

@interface Object : NSObject {
    NSString *color;
}    
@property(nonatomic, retain) NSString *color;
@end

Object.m:

#import "Object.h"
@implementation Object
@synthesize color;
@end

I wasn’t able to define an object myObject in ViewDidLoad so that I can access its properties from the whole ViewController class? What did I miss?
Side question: Why do I have to set myObject to 0?

  • 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-14T04:38:14+00:00Added an answer on May 14, 2026 at 4:38 am
    1. You’re declaring a property, then explicitly declaring the accessors in Object.h. You only need to do one or the other – they mean the same thing (well, almost – you’ll have color instead of getColor)
    2. To implement the property in Object.m you should use @synthesize color. The explicit implementations, again, are then redundant (unless they do anything extra).
    3. The explicit setColor implementation in Object.m is calling the property – which you are implementing explicitly, so I would have expected you to get an infinite recursion here.
    4. MyViewController.m should probably synthesize label1, since you declare the property in the header (although it’s not being used in your snippet).
    5. [myObject getColor] is calling the color property, which you declared but did not synthesize. If you had explicitly implemented it as color it would have picked that up – but it won’t match getColor (which is fortunately as that would have led to an infinite recursion again.
    6. I don’t see anywhere where you create your myObject instance. If you don’t it will be nil and methods called on it (including property accesses) will return 0 or nil.

    I suspect (6) is the cause of your issue, but the others need to be addressed too. Make sure you read up on property syntax.

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

Sidebar

Related Questions

I simply want to generate a class with attributes that comes from a Database
I simply want to return all rows from a users table but selected fields,
I simply want to fill-up cells in my spreadsheet from a VBA function. By
Hey guys I have an assigned array from mysql results and I simply want
I heard its hard but - possible. I simply want to do it cauz
So for reason of running many projects, I don't want to change JAVA_HOME variable
I simply want to show off a model in a table that is paginated
I have several sql queries that I simply want to fire at the database.
I'm working with various XML inputs and outputs and simply want to grab the
This supposedly easy task gave me some headache. I simply want to let the

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.