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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:31:58+00:00 2026-05-22T15:31:58+00:00

About a year ago, when I first got into programming, I learned the hard

  • 0

About a year ago, when I first got into programming, I learned the hard way that variables don’t escape the scope of the condition they’re created in. For example:

-(void)someMethod {
    if (x == y) {
        NSString *string = [[NSString alloc] initWithString:@"Foo"];
        NSLog(string); // outputs "Foo" to console successfully
    }
    ...
    NSLog(string); // Doesn't work, we're out of the scope of the "if" statement.
}

My question is, is there any way to dynamically create variables within a conditional statement and be able to access them at other times, kind of like if I declared it in my @interface?

EDIT I guess I didn’t explain it well, I meant if I wanted to use it later in other methods.

  • 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-22T15:31:59+00:00Added an answer on May 22, 2026 at 3:31 pm

    You just need to declare (and optionally initialize) the variable outside of the if. So something like:

    -(void)someMethod {
    
        NSString *string = nil;
        if (x == y) {
            string = [[NSString alloc] initWithString:@"Foo"];
            NSLog(string); // outputs "Foo" to console successfully
        }
        ...
        NSLog(string); 
    }
    

    EDIT

    To respond to your clarification, ‘string’ here is a local variable, local to this method. You have other options like instance variables for example. Instance methods like this one (ones that start with ‘-‘) will have access to this instance’s (this object’s) instance variables.

    So you could do:

    @interface MyClass : NSObject {
    
        NSString *string; // <<-- This is an instance variable (aka "ivar")
    }
    
    - (void)someMethod;
    - (void)someOtherMethod;
    @end
    
    @implementation MyClass
    
    - (void)someMethod {
    
        string = @"Foo";
    }
    
    - (void)someOtherMethod {
    
        NSLog (string); 
       // will print "Foo" provided someMethod was called before this method
    }
    
    
    @end
    

    Obviously there’s more to this than you can get in a short answer. You can have global variables. If you’re new to the language, you should read properties as a very useful tool for encapsulating instance variables (useful when you want to get the memory mgmt right). But hopefully that gets you pointed in the right direction at least.

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

Sidebar

Related Questions

About half year ago I decided to improve my programming efficiency, so I learned
I've got a website that was created about an year ago and its been
about a year ago I stumbled across a nice feature in Java that I
About a year ago I picked up my first HTML book (XHTML). I have
About a year ago someone helped me to setup a file that would pipe
I asked this question about a year ago on another site but never got
About a year ago, a manager in another department brainstormed that I could code
I used SPRY about a year ago to experiment with the fact that you
I started programming in C# and ASP.NET about a year ago. When I was
About a year ago we are developed for internal use inprocess MOLAP engine that

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.