简体   繁体   中英

Is the @ operator in Objective-C same as the & operator in C?

New to objective C. Trying to understand pointers in Objective C.

my code

#import <Foundation/Foundation.h>

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *msg = @"Hello Bhai";
NSLog(@" address is %i for msg", msg);
NSLog(@" which is same address  %i for Hello Bhai", @"Hello Bhai");
NSLog(@" which is different address  %i for Hello mere Bhai", @"Hello mere Bhai");
NSLog(@" value is %@ at the address for msg", msg);


[pool drain];
    return 0;
}

output is

[Session started at 2011-04-26 16:31:11 -0400.]
2011-04-26 16:31:11.656 BasicObjC[1523:10b]  address is 8240 for msg
2011-04-26 16:31:11.658 BasicObjC[1523:10b]  which is same address  8240 for Hello Bhai
2011-04-26 16:31:11.659 BasicObjC[1523:10b]  which is different address  8288 for Hello mere Bhai
2011-04-26 16:31:11.660 BasicObjC[1523:10b]  value is Hello Bhai at the address for msg

Does this mean @ in Objective C is the same as & in C

@ is used to denote that string is a NSString literal so you can assign it to NSString object.

If you want to print some address then you should use %p format specifier, not %i, eg

NSLog(@" address is %p for msg", msg);

Does this mean @ in Objective C is the same as & in C

No, in this context the '@' is used to differentiate an NSString literal from a C char array so that you can choose to use objective-c NSStrings instead of C strings.

Within a format string '%@' is used to get the string representation of an NSObject (from its "description" method) so you can output it to the console.

You are also running into a special behavior of NSString literals. Both of your @"Hello Bhai" string pointers reference the same memory address only because NSString is optimized not to create identical copies of string literals. Other objects will not necessarily behave the same way. In general you shouldn't need to be aware of this sort of optimization because should not make any difference in your use of these objects. If you correctly follow the same retain/release rules and comparisons as with any other object then you'll never notice the difference.

The at sign ( @ ) is something completely different from the & operator. The @"…" syntax is a shorthand for creating an NSString instance from characters given in the quotes.

In general, you will see the @ sign used many places in Objective-C, for syntax that is specific to that language and thus not legal in "plain" C:

@implementation
@interface
@property
@selector
@class

As noted by just about everyone else, in this specific case, @"This is a string." , it is used to indicate the start of an Objective-C string literal. It is also used as a format specifier %@ to mean "object".

@ was originally chosen for these purposes because it wasn't used for anything in C. The "address-of" operator & is perfectly legal in Objective-C*, because Obj-C is a superset of C.


*In fact, it gets used in Cocoa fairly often for indirect returns of values; see for example Error Handling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM