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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:58:45+00:00 2026-05-17T17:58:45+00:00

In my controller class, I initialize two instances of a model class (whose header

  • 0

In my controller class, I initialize two instances of a model class (whose header is properly imported into controller class) with an NSButton. The model is really simple, just 4 members and one method – attack(). Making a silly text game!

 - (IBAction)startGame:(id)sender {

    Combatant *hero = [[Combatant alloc] init];
    Combatant *enemy = [[Combatant alloc] init];
    [console insertText:@"You have created a hero! An enemy approaches...\n"];
 }

So now I have these two objects sitting there. Or do I? Because this other button, the one that’s supposed to make them fight, has no idea what hero and enemy are, or that they have a class method that makes em’ fight!

- (IBAction)attack:(id)sender{

[hero attack:enemy]; //Use of undeclared identifier, blah blah.
[console insertText:@"You attack the enemy! Woah!\n"];}

I get that if I initialized those objects in the attack method, then I could use them, so I gather this is something to do with scope. But I don’t like the idea of sending model objects to controller methods, that seems silly.

Let me apologize: yes, this is a stupid, high-level question about the structure of Cocoa. Sorry. But I figure one of you will know exactly what I am not doing and tell me to do it!

In short, what is the Cocoa way of doing things in this situation? Thanks in advance.

-Alec

  • 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-17T17:58:46+00:00Added an answer on May 17, 2026 at 5:58 pm

    When you declare a variable in a method, it is a local variable, which means it only exists in that method. The same goes for variables you declare in functions.

    If you want the variable to exist in all instance methods in the class, you need to make it an instance variable, which you do by declaring it in that { … } section in the class’s @interface.

    Note that any objects you store in instance variables, the instance should own. This means three things:

    1. You’ll need to either retain the object (and thereby own it) or make a copy (which you will then own) before assigning it to the instance variable.
    2. Since you own it, you’ll need to release it in the instance’s dealloc method.
    3. If you decide to replace it with a different object, you’ll need to release the former object (since you still own it) and retain or copy the new object (in order to own it).

    See the Objective-C Programming Language and the Memory Management Programming Guide for more information.

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

Sidebar

Related Questions

In my Rails controller, I'm creating multiple instances of the same model class. I
How do you put two different function outputs from the same controller into the
My Controller class is decorated with an AuthorizeAttribute to protect the actions: [Authorize(Roles =
I have an Apex class (controller) originally developed under Developer Edition and need to
I have a controller with an action method as follows: public class InventoryController :
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I am trying to use UpdateModel in a class other than my controller and
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
Suppose the following route: {region}/{storehouse}/{controller}/{action} These two parameters region and storehouse altogether identify a
I've got a two vectors in class A that contain other class objects B

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.