uitableviewcell - uitableview with sections objective c
Length of an Array in Objective C (8)
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.
@Sedate Alien is right.
[myArray count] or
myArray.count are the ways to get the length.
It looks like you might be missing something simple like the framework containing
NSArray ... not sure how, since most templates include it, but just in case make sure you have
#import <Foundation/Foundation.h> and added the actual framework to your project
Project->Build Phases->Link Binary With Libraries... -> "+" button->Foundation)
An answer to another answer:
You can't get the size of the array in megabytes, at least not without doing some tricky, unsupported* C voodoo.
NSArray is a class cluster, which means we don't know how it's implemented internally. Indeed, the implementation used can change depending on how many items are in the array. Moreover, the size of the array is disjoint from the size of the objects the array references, since those objects live elsewhere on the heap. Even the structure that holds the object pointers isn't technically "part" of the array, since it isn't necessarily
calloc'd right next to the actual
NSArray on the heap.
If you want the size of the array struct itself, well that's apparently only 4 bytes:
NSLog(@"Size: %d", sizeof(NSArray));
2010-03-24 20:08:33.334 EmptyFoundation[90062:a0f] Size: 4
(Granted, that's not a decent representation, since
NSArray is probably just an abstract interface for another kind of object, usually something like an
NSCFArray. If you look in
NSArray.h, you'll see that an
NSArray has no instance variables. Pretty weird for something that's supposed to hold other objects, eh?)
* By "unsupported" I mean "not recommended", "delving into the inner mysticism of class clusters", and "undocumented and private API, if it even exists"
The C mentality is to provide the programmer with tools to help him with his job, not to provide abstractions which change the nature of his job. C also tries to avoid making things easier/safer if this happens at the expense of the performance limit.
Certain things you might like to do with a region of memory only require the location of the start of the region. Such things include working with null-terminated strings, manipulating the first n bytes of the region (if the region is known to be at least this large), and so forth.
Basically, keeping track of the length of a region is extra work, and if C did it automatically, it would sometimes be doing it unnecessarily.
Many library functions (for instance
fread()) require a pointer to the start of a region, and also the size of this region. If you need the size of a region, you must keep track of it.
Yes, malloc() implementations usually keep track of a region's size, but they may do this indirectly, or round it up to some value, or not keep it at all. Even if they support it, finding the size this way might be slow compared with keeping track of it yourself.
If you need a data structure that knows how big each region is, C can do that for you. Just use a struct that keeps track of how large the region is as well as a pointer to the region.
There is no standard way to find this information. However, some implementations provide functions like
msize to do this. For example:
Keep in mind though, that malloc will allocate a minimum of the size requested, so you should check if msize variant for your implementation actually returns the size of the object or the memory actually allocated on the heap.
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.
sz = (sizeof foo) / (sizeof foo);
You can use [XYZ count] to get the length of the array
Length of Allocated Array
What is length? If you want to find the number of objects in the array use:
If you want to find the size, check this link out:
Size of an NSArray
int size = [array count]; NSLog(@"there are %d objects in the array", size);