简体   繁体   中英

What is the difference between 4 and @4?

I just started learning this, and the tutorial started off by using the @ symbol before all number literals, and string literals, and I thought "Okay, Objective-C uses the @ symbol before literals," but then next thing you know they used some numbers without the @ sign and I was at a total loss.

(I'm saying 'number' because I'm afraid to say int since Objective-C / C has so many types of number.)

What's the difference?

Objective C is an extension of C, so it uses regular literals in places where C uses literals - ie for providing values of primitive types int , long , etc.

In addition, Objective C has support for NSNumber class of the Cocoa framework. Objects of this class are used to wrap primitive values for uses where an object is required - for example, to be put in a collection. Cocoa collections do not accept values of primitive types, so you need to provide an object wrapper before placing a numeric value into a collection.

Objective C did not have support for making literals of type NSNumber , so you needed to wrap literals manually, like this:

[NSNumber numberWithInt:4]

This is too much typing, especially when you need to define multiple such values to put into a collection. That is why Objective C added an alternative syntax for creating NSNumber s - the one with the @ sign. So when you write @4 , that's the same as writing [NSNumber numberWithInt:4] , but is a lot less typing. For example, an initialization that looked like this in the old syntax

NSArray *oneTwoThree = [NSArray arrayWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];

now looks like

NSArray *oneTwoThree = @[@1, @2, @3];

which is a lot more readable.

Note: the second code snippet uses the new syntax for array initialization as well.

There is a difference between a int type and an NSNumber object. An int is a data type used to store integers. You create an int like this: int i = 4;

An NSNumber is an Objective-C object that can store any type of number, and int, double, float, short, long, etc. This is created like NSNumber *num = @4; , which actually becomes NSNumber *num = [NSNumber numberWithInt: 4];

The main reason to use @4 instead of 4 is in NSArrays and NSDictionary's. You can only store objects, not types like int or float, in these objects. So if you want an array of numbers, it would have to be like [NSArray arrayWithObjects: @1, @2, @3, @4, nil];

The @ has other uses as you have realized. Just like how a number becomes an object by adding the @ to it, a C-String "Hello World" becomes an Objective-C object (NSString) with @"Hello World" . A C-Array [1,2,3,4] becomes an Obj-C object (NSArray) with @[@1,@2,@3,@4] . Same with dictionaries: @{key,value}

If you want to learn more about literals, you can use this link , or this link .

One is a number (an int, a scalar). The other is an NSNumber (an Objective-C object; on this notation, see http://clang.llvm.org/docs/ObjectiveCLiterals.html ).

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