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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:55:21+00:00 2026-06-03T12:55:21+00:00

So I have a class defined in Foo.h, implemented in Foo.m. I then have

  • 0

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!

  • 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-03T12:55:25+00:00Added an answer on June 3, 2026 at 12:55 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a serial of object defined as: public class Foo { public DateTime
I have a class defined like this, with the appropriate getter and setter methods...
I have a class that defined a user defined operator for a TCHAR*, like
I have a class with defined functions that need to be passed as parameters.
I have a class Foo defined like this: class Foo { public: Foo(int num);
Assuming I have a class B derived from A A defined foo() and also,
I have a EJB defined as this: package com.foo; @Stateless (mappedName=HelloWorld) public class HelloWorldBean
I have an example class defined like below: public class FooBar { void method1(Foo
I have the following classes defined: public interface Thingy { ... } public class
I have a class that already has a 'natural' order, and wish to define

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.