简体   繁体   English

4和@ 4有什么区别?

[英]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. 我刚刚开始学习这个,并且教程从所有数字文字和字符串文字之前的@符号开始,我想“好吧,Objective-C在文字之前使用@符号”,但接下来你知道他们使用了一些没有 @符号的数字,我完全失去了。

(I'm saying 'number' because I'm afraid to say int since Objective-C / C has so many types of number.) (我说的是'数字',因为我害怕说int,因为Objective-C / C有很多类型的数字。)

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. Objective C是C的扩展,因此它在C使用文字的地方使用常规文字 - 即提供原始类型intlong等的值。

In addition, Objective C has support for NSNumber class of the Cocoa framework. 此外,Objective C支持Cocoa框架的NSNumber类。 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. Cocoa集合不接受基本类型的值,因此在将数值放入集合之前需要提供对象包装器。

Objective C did not have support for making literals of type NSNumber , so you needed to wrap literals manually, like this: Objective C不支持创建NSNumber类型的文字,因此您需要手动包装文字,如下所示:

[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. 这就是为什么Objective C为创建NSNumber添加了另一种语法 - 带有@符号的语法。 So when you write @4 , that's the same as writing [NSNumber numberWithInt:4] , but is a lot less typing. 所以当你写@4 ,这与编写[NSNumber numberWithInt:4] ,但输入的次数要少得多。 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. int类型和NSNumber对象之间存在差异。 An int is a data type used to store integers. int是用于存储整数的数据类型。 You create an int like this: int i = 4; 你创建一个像这样的int: 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; NSNumber是一个Objective-C对象,可以存储任何类型的数字,以及int,double,float,short,long等。这个创建类似于NSNumber *num = @4; , which actually becomes NSNumber *num = [NSNumber numberWithInt: 4]; ,实际上变成NSNumber *num = [NSNumber numberWithInt: 4];

The main reason to use @4 instead of 4 is in NSArrays and NSDictionary's. 使用@4而不是4主要原因是在NSArrays和NSDictionary中。 You can only store objects, not types like int or float, in these objects. 您只能在这些对象中存储对象,而不是int或float等类型。 So if you want an array of numbers, it would have to be like [NSArray arrayWithObjects: @1, @2, @3, @4, nil]; 所以如果你想要一个数组,它必须像[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" . 就像数字如何通过向它添加@来成为对象一样,C-String "Hello World"成为带有@"Hello World"的Objective-C对象(NSString)。 A C-Array [1,2,3,4] becomes an Obj-C object (NSArray) with @[@1,@2,@3,@4] . C-Array [1,2,3,4]成为具有@[@1,@2,@3,@4]的Obj-C对象(NSArray)。 Same with dictionaries: @{key,value} 与词典相同: @{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). 一个是数字(一个int,一个标量)。 The other is an NSNumber (an Objective-C object; on this notation, see http://clang.llvm.org/docs/ObjectiveCLiterals.html ). 另一个是NSNumber(一个Objective-C对象;在这个表示法上,请参阅http://clang.llvm.org/docs/ObjectiveCLiterals.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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