objective-c 目标 C 中数组的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1530050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:19:45  来源:igfitidea点击:

Length of an Array in Objective C

objective-carraysfind

提问by Darpan

I haven't found answer in any of the questions asked. I am passing an Integer Array to a function.Now I want to traverse through the array.But as things worked in C,C++ by simple using arrayname.length which gave the number of elements in array. What is the way to find that? [NSArrayObject length] works for NSArray type but I want it for int[] . not even [XYZ count] works....so want another way to find that out.

我在提出的任何问题中都没有找到答案。我正在将一个整数数组传递给一个函数。现在我想遍历该数组。但是正如 C、C++ 中的事情一样,通过简单地使用 arrayname.length 给出了数组中元素的数量。找到那个的方法是什么?[NSArrayObject length] 适用于 NSArray 类型,但我希望它适用于 int[] 。甚至 [XYZ count] 都不起作用……所以想要另一种方法来找出答案。

采纳答案by Andy Gaskell

There isn't anything specific to Objective-C with an array of ints. You would use the same technique as if you were using C.

没有任何特定于带有整数数组的 Objective-C。您将使用与使用 C 相同的技术。

sz = (sizeof foo) / (sizeof foo[0]);

回答by user2070420

You can use [XYZ count] to get the length of the array

您可以使用 [XYZ count] 来获取数组的长度

回答by Chuck

There is no such thing as array.lengthin C. An int array in Objective-C is exactly the sameas an int array in C. If it's statically defined like int foo[5], then you can do sizeof(foo)to get the size — but only in the same function foois defined in (to other functions, it's just an int pointer, not an array per se). Otherwise, there is no inherent way to get this information. You need to either pass the size around or use sentinel values (like the terminating '\0' in C strings, which are just arrays of char).

array.lengthC 中没有这样的东西。Objective-C 中的 int 数组与 C 中的 int 数组完全相同。如果它是静态定义的int foo[5],那么您可以sizeof(foo)获取大小 - 但只能在相同的函数foo中定义in(对于其他函数,它只是一个 int 指针,而不是数组本身)。否则,没有固有的方式来获取这些信息。您需要传递大小或使用标记值(如 C 字符串中的终止 '\0' ,它们只是字符数组)。

回答by unwind

Huh? In C, there's no such thing as "arrayname.length". An array of a primitive type, such as int[], does not have any length information associated with it at runtime.

嗯?在 C 中,没有“arrayname.length”这样的东西。原始类型的数组,例如int[],在运行时没有任何与之关联的长度信息。

回答by chillininvt

[array count] this appears to work the easiest in objective-c

[数组计数] 这似乎在objective-c 中最简单

回答by MBANZABUGABO Jean Bapitiste

this code can be use when the total number of element in array are not known.

当数组中的元素总数未知时,可以使用此代码。

main()
{
    int a[]={1,2,3,4,5,6,7}
    int i;
    clrscr();
    for (i=0;i<=((sizeof(a)/sizeof(int));i++)
    {
        puts(a[i]);
    }
    getch();
}

回答by PeyloW

There is no such thing as arrayname.lengthin C. That is why so many functions take argument pairs, one argument with the array and one argument with the length of the array. The most obvious case for this is the main function. You can find this function is all your iPhone projects in a file named main.m, it will look something like this:

arrayname.length在 C 中没有这样的东西。这就是为什么这么多函数采用参数对,一个参数是数组,一个参数是数组的长度。最明显的例子是 main 函数。你可以在一个名为 的文件中找到这个函数是你所有的 iPhone 项目main.m,它看起来像这样:

int main(int argc, char *argv[]) {    
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc, argv, nil, nil);
  [pool release];
  return retVal;
}

Notice how the argvagument is a vector, or array of strings, and the argcargument is a count of how many items that are in this array.

注意,此时的argvagument是v埃克特,或字符串数组,并且argc参数是一个ç的有多少项目是该数组中的'mount。

You will have to do the same thing for all primitive types, this is the Cpart of Objective-C. If you stay in the Objectiveparts using NSArray or subclasses works fine, but requires all elements to be objects.

你必须对所有原始类型做同样的事情,这是Objective-C的C部分。如果你留在使用 NSArray 或子类的目标部分工作正常,但要求所有元素都是对象。

回答by pxl

looks like a job for NSMutableArray. is there a reason why you need to work with a C array? if not, consider NSMutableArray.

看起来像是一份工作NSMutableArray。有什么理由需要使用 C 数组吗?如果没有,请考虑NSMutableArray

i may be wrong (so please correct me if i am), but there's no easy way in C to get the size of int[] at runtime. you would have to do something like create a struct to hold your array and an int where you can keep track of how many items in your array yourself.

我可能错了(所以如果我错了,请纠正我),但是在 C 中没有简单的方法可以在运行时获取 int[] 的大小。你必须做一些事情,比如创建一个结构来保存你的数组和一个 int ,你可以在其中自己跟踪数组中有多少项。

even fancier, you can make your struct hold your array and a block. then you can design the block to do the tracking for you.

甚至更高级,你可以让你的结构保存你的数组和一个块。然后您可以设计块来为您进行跟踪。

but this is more a C question, not an objective-c quesiton.

但这更像是一个 C 问题,而不是一个客观的 C 问题。

回答by Kendall Helmstetter Gelner

If you want an array of integers, wrap them in NSNumber objects and place them into an NSArray.

如果您想要一个整数数组,请将它们包装在 NSNumber 对象中并将它们放入 NSArray 中。

What you are asking to do, is not really supported well in C though there is a method on the mac you could try (malloc_size):

尽管您可以尝试在 mac 上使用一种方法(malloc_size),但您要求执行的操作在 C 中并没有得到很好的支持:

determine size of dynamically allocated memory in c

确定c中动态分配内存的大小

回答by RichApple

You should try the following approach:

您应该尝试以下方法:

float ptArray[] = {97.8, 92.3, 89.4, 85.7, 81.0, 73.4, 53.0, 42.3, 29.4, 14.1};

int x = sizeof(ptArray)/sizeof(ptArray[0]);

 NSLog(@"array count = %d", x);

Output:

输出

array count = 10