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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:50:03+00:00 2026-06-11T04:50:03+00:00

Take a look at this: #import <Foundation/Foundation.h> @interface ClassA : NSObject -(NSString *) method1:(NSString*)str1;

  • 0

Take a look at this:

#import <Foundation/Foundation.h>

@interface ClassA : NSObject

-(NSString *) method1:(NSString*)str1;

@end

@implementation ClassA

-(NSString *) method1:(NSString*)str1
{
    NSLog(@"2. %@ at %p", str1, str1);
    str1 = @"foo";
    NSLog(@"3. %@ at %p", str1, str1);  
    return str1;
}

@end


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // insert code here...
        NSLog(@"Hello, World!");
        NSString *str = @"moo";
        NSLog(@"1. %@ at %p", str, str);
        ClassA *a = [[ClassA alloc] init];
        NSString *b = [a method1:str];
        NSLog(@"4. %@ at %p", str, str);
        NSLog(@"5. %@ at %p", b, b);
    }
    return 0;
}  

The console log is:

2012-09-11 17:03:16.160 passByValue[1559:403] Hello, World!
2012-09-11 17:03:16.162 passByValue[1559:403] 1. moo at 0x104c42248
2012-09-11 17:03:16.162 passByValue[1559:403] 2. moo at 0x104c42248
2012-09-11 17:03:16.163 passByValue[1559:403] 3. foo at 0x104c421e8
2012-09-11 17:03:16.163 passByValue[1559:403] 4. moo at 0x104c42248
2012-09-11 17:03:16.164 passByValue[1559:403] 5. foo at 0x104c421e8

Notice the address of str is the main function and the address of str1 in method1 is same until str1 is reassigned. Does this follow the principle of passing by Value?

Yes, the value and address of str remains the same after the control comes back to main which follows the principle of passing by value.

So can anyone explain this, or should we live with the fact that str is passed by value and not by reference.

  • 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-11T04:50:05+00:00Added an answer on June 11, 2026 at 4:50 am

    First of all, “objects” are not values in Objective-C. You cannot have an expression of “object” type — the compiler would complain if you did something like this:

    NSString foo;
    

    Rather, you can only have object pointers. Everything you can do to objects, you do through object pointers. In your code, str, str1, etc. are object pointer variables.

    With that out of the way, Objective-C is indeed always pass-by-value. Your question doesn’t really make sense because “objects” are not passed or assigned. (“Objects” are not values in the first place.) In your example, your methods take object pointers.

    As always with pass-by-value, assignments to local variables do not affect outside code.

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

Sidebar

Related Questions

Take a look at this code : public class Program { [import]IMain Main {get;
Please, take a look at this example: import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Test
take look at this: http://www.templatemonster.com/demo/35403.html In this template you can click on the image
Take a look at this example here: http://denise.brixwork.com/showlisting/1/8297-Valley-Drive-Alpine-Meadows-Whistler-denise-brown-real-estate And the red tables under Specifications
Take a look at this: http://thebekker.dk/_skole/GFeksamen/ You can see the 2nd menu item show
Take a look at this with IE or Chrome and notice the yellow block:
Take a look at this site . When you scroll down, it automatically loads
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
Take a look at this code: public class Test { public static void main(String...
Take a look at this table: <table cellpadding=0 cellspacing=0 class=order_form> <tr> <th>Amount</th> <th>Desc</th> <th>Price</th>

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.