brianSan
brianSan

Reputation: 535

Value of NSString property doesn't remain after setting

So I have a class defined in Foo.h, implemented in Foo.m. I then have a controller called FooController.h/m that has multiple methods to interface Foo with the view.

Foo has multiple properties so I'm only gonna refer to three defined as

// Foo.h

    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSNumber *level;
    @property (strong, nonatomic) Foo1 *foo1;

As you can see, Foo has a property of class Foo1 which I defined elsewhere. I didn't complete the implementation of Foo1 but basically it's just a set of more properties that are defined. (I didn't define any init method for Foo1. Could that be a problem?) One property of Foo1 that you should keep in the back of your mind is:

// Foo1.h

    @property (strong, nonatomic) NSString*name;

The following Foo methods are defined:

// Foo.m

    -(id) initWithName:(NSString *)name
                 level:(NSNumber *)level
                  foo1:(Foo1 *)foo1
    {
       self = [super init];
       if (self) {
         _name = name;
         _level = level;
         _foo1 = foo1;
         return self;
       }
       return nil;
    }

One of the methods that FooController has is called makeRandomFoo that generates a generic name, random level, and a statically defined instance of foo1 (static in the sense of size). It's implementation is as follows:

// FooController.h

    #import "Foo.h"
    #import "Foo1.h"

    @interface FooController : NSObject

    @property (strong, nonatomic) Foo *foo;

    - (Foo *)makeRandomFoo:(Foo *)foo;

// FooContoller.m

    #import <stdlib.h>

    @implementation FooController

    @synthesize foo = _foo;

    - (Foo *)makeRandomFoo:(Foo *)foo
    {
      NSString *name = @"Random Foo 1";
      NSNumber *level = [NSNumber numberWithInt:(rand() / 100)]; 
      Foo1 *foo1 = [[foo1 alloc] init];
      foo = [[foo alloc] initwithName:name
                              atLevel:level
                             withFoo1:foo1];
      return foo;
    }

Then on my viewcontroller, FooViewController.h/m, I created a round rect button called "Make Random Foo" and three labels called "nameLabel", "levelLabel" "foo1Label"

Before I show "Make Random Foo"'s method (bottom of the implementation), I should just show the definition and implementation of my vc:

// FooViewController.h

    @class Foo;
    @class FooController;

    @interface FooViewController : UIViewController

    @property (strong, nonatomic) Foo *foo;
    @property (strong, nonatomic) FooController *fooController;
    @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
    @property (weak, nonatomic) IBOutlet UILabel *levelLabel;
    @property (weak, nonatomic) IBOutlet UILabel *foo1Label;

    - (IBAction)randomizeButton;

// FooViewController.m

    - (id)reloadView
    {
       self.nameLabel.text = self.pet.name;
       self.levelLabel.text = [NSString stringWithFormat:@"%@",self.pet.level];
       self.foo1Label.text = self.pet.foo1.name;
       (remember when I told you about a property of foo1 called name?)
    }

    - (IBAction)randomizeButton
    {
      [self.petController makeRandomFoo:self.foo];
      [self reloadView];
    }

So when I run the code and hit the random button, the name and foo1 label remains blank and the level label says (null). All properties are synthesized but I didn't write and custom setters or getters for them. Could you help me out?

I tried being as descriptive as I could but if you have more questions, feel free to ask away!

Upvotes: 0

Views: 1105

Answers (2)

trojanfoe
trojanfoe

Reputation: 122401

You are not following memory management rules as you are assigning the initialisation parameters directly to your instance variables within [Foo init]. You need to use your (synthesized) setter methods instead:

-(id) initWithName:(NSString *)name
                 level:(NSNumber *)level
                  foo1:(Foo1 *)foo1
{
   self = [super init];
   if (self) {
     self.name = name;
     self.level = level;
     self.foo1 = foo1;
   }
   return self;
}

As a consequence of your implementation the instance variables are not being retained and are therefore probably being released prematurely. We don't want premature anything now do we?

EDIT To implement your setter methods (that have been declared using the @property keyword in the header file), you simply use @synthensize the same way you have done in FooController already:

@implementation Foo

@synthesize name = _name;
@synthesize level = _level;
@synthesize foo1 = _foo1;

-(id) initWithName:(NSString *)name
... etc ...

@end

EDIT 2: After a bit more looking, you seem to be calling the [Foo init...] method with differently named parameters. You also need to release the Foo1 object as you have passed ownership to Foo. Also conventionally you need to start this method with the name new as you are creating and returning a new instance of Foo.

Try this and set a breakpoint at the last line and see if you can inspect the properties of Foo:

- (Foo *)newRandomFoo:(Foo *)foo
{
    NSString *name = @"Random Foo 1";
    NSNumber *level = [NSNumber numberWithInt:(rand() / 100)]; 
    Foo1 *foo1 = [[foo1 alloc] init];
    foo = [[foo alloc] initwithName:name
                              level:level
                               foo1:foo1];
    [foo1 release];
    return foo;
}

Upvotes: 3

jrturton
jrturton

Reputation: 119272

This is what I think you want to do, based on my guesses (you seem to have replaced "Pet" with "Foo" in most, but not all of your sample code, so it is pretty hard to follow. Why not just post your real code?)

makeRandomFoo shouldn't take a parameter, so should just look like this:

- (Foo *)makeRandomFoo
    {
      NSString *name = @"Random Foo 1";
      NSNumber *level = [NSNumber numberWithInt:(rand() / 100)]; 
      Foo1 *foo1 = [[Foo1 alloc] init];
      Foo *foo = [[Foo alloc] initwithName:name
                              atLevel:level
                             withFoo1:foo1];
      return foo;
    }

When you call it from your view controller, It looks like you actually want to replace the view controller's foo with your "random" one, so it should be:

- (IBAction)randomizeButton
    {
      self.foo = [self.petController makeRandomFoo];
      [self reloadView];
    }

Last of all, depending on the implementation of Foo1 the name property will not contain anything unless you have a default in the accessor, since all you are doing is alloc / initing it and you have said that you do not have an overridden init method.

Upvotes: 0

Related Questions