简体   繁体   English

Objective-C中的方括号和大括号之间有什么区别?

[英]What is the difference between square brackets and curly brackets in Objective-C?

I am new to Objective-C and can not understand the difference in using curly brackets and square brackets for arrays? 我是Objective-C的新手,无法理解在数组中使用大括号和方括号的区别吗? For example below code 例如下面的代码

float trainingData[3][3] = { { 84, 191, 19 }, { 24, 186, 17}, { 22, 157, 21} };

you have a multi dimensional array or jaggad array,which means an array of arrays... 您有一个多维数组或jaggad数组,这意味着一个数组数组...
this means you have....................................with values 这意味着你有..................的价值

array1: x1 x2 x3                            84,191,19
array2: x4 x5 x6                            24,186,17
array3: x7 x8 x9                            22,157,21

now the curly brackets represents which area you are adding 现在大括号代表您要添加的区域

so if you add row 1(or array 1) ... trainingData[0] 因此,如果您添加第1行(或数组1)... trainingData[0]
It requires an array of values(a row has multiple values) 它需要一个值数组(一行具有多个值)

So the curly brackets mean you are assigning an object in its index 因此,大括号表示您正在为其索引分配对象
for example if you want an array with values 1,2,3 you would do something like 例如,如果您想要一个值为1,2,3的数组,则可以执行以下操作

int[] values = {1,2,3}

now in your case you have an array of array's so the first(or outside) curly brackets are for defining each row and the curly brackets within them is to define each row's values inside them 现在,在您的情况下,您有一个数组的数组,因此第一个(或外部)大括号用于定义每行,而其中的大括号用于定义其中的每一行的值
example

float trainingData[3][2] = { { 84, 191}, { 24, 186}, { 22, 157} };

note now the each row only has 2 values, because the array length has changed so you have 3 groups of 2 注意现在每行只有2个值,因为数组长度已更改,所以您有3组2

array1: 84,191
array2: 24,186
array3: 22,157

In C: 在C中:

Square Bracket: It represent that it is an Array. 方括号:表示它是一个数组。

In your given code trainingData[3][3] represent the a 2D Array with 3 row and 3 column. 在给定的代码中, trainingData [3] [3]表示一个3行3列的2D数组。

Curly Bracket: Multidimensional arrays may be initialized by specifying bracketed values for each row. 弯括号:可以通过为每行指定带括号的值来初始化多维数组。 In your Given code: 在给定的代码中:

float trainingData[3][3] = { 
{ 84, 191, 19 },       /*  initializers for row indexed by 0 */
{ 24, 186, 17},         /*  initializers for row indexed by 1 */
{ 22, 157, 21}          /*  initializers for row indexed by 2 */
};

In Objective C: 在目标C中:

Square Bracket: 方括号:

the square bracket syntax use for all of method calls. 方括号语法用于所有方法调用。 you use the dot syntax when you're using getters and setters. 使用getter和setter时,您可以使用点语法。

Curly Bracket: It Represent the dictionary or mutable dictionary. 弯括号:代表字典或可变字典。

NSDictionary *Dic= @{@"Name":@"abc",@"ID":@"123"};

You can says that declaration or definition take places in square brackets and values assigns in curly brackets. 您可以说声明或定义放在方括号中,而赋值在大括号中。

for example, 例如,

double balance[5];

This is the array of size 5 , so that 5 is defined in square brackets . 这是大小为5的数组,因此5被定义在square brackets

Now, 现在,

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

then value is define in curly bracket. 然后在大括号中定义值。

balance[4] = 50.0;

setting 4th element, so index is define in square bracket. 设置第四个元素,因此索引定义在方括号中。 Here there is a single value so curly bracket is not require. 这里只有一个值,因此不需要大括号。 So, basically use of curly bracket is to declare or assign set. 因此,大括号的基本用途是声明或分配集合。 As you have post array in your question is also value set of array. 由于您的问题中有数组,数组的值也是一组。

so, you can differentiate it like i have mentioned. 因此,您可以像我提到的那样区分它。

Actually these are the protocols or syntax you said to use array, so finding difference between use of braces is meaningless actually. 实际上,这些是您说要使用数组的协议或语法,因此,发现花括号之间的区别实际上是没有意义的。

float trainingData[3][3] is a multi dimentional array you are having and you are saying trainingData multi dimentional array is of size 3 : 3 within the square brackets float trainingData [3] [3]是一个多维数组,您说的是trainingData多维数组,在方括号中的大小为3:3

within the curly braces { { 84, 191, 19 }, { 24, 186, 17}, { 22, 157, 21} }; 在花括号{{84,191,19},{24,186,17},{22,157,21}}中;

you are assigning values for the array 您正在为数组分配值

[ 3 : 3 ] [3:3]

trainingData{0} => { 84, 191, 19 } trainingData {0} => {84,191,19}

trainingData{1} => { 24, 186, 17} trainingData {1} => {24,186,17}

trainingData{3} => { 22, 157, 21} trainingData {3} => {22,157,21}

Usually you define the size with square brackets and assing them with curly braces 通常,您使用方括号定义大小,并使用花括号将其添加

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

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