Showing posts with label getObjects. Show all posts
Showing posts with label getObjects. Show all posts

Friday, May 31, 2013

NSArray getObjects example in Objective C (iOS).


NSArray getObjects


Copies the objects contained in the array that fall within the specified range to aBuffer.

- (void)getObjects:(id[])aBuffer range:(NSRange)aRange

Parameters of [NSArray getObjects]
aBuffer
A C array of objects of size at least the length of the range specified by aRange.
aRange
A range within the bounds of the array.
If the location plus the length of the range is greater than the count of the array, this method raises an NSRangeException.

Discussion of [NSArray getObjects]
The method copies into aBuffer the objects in the array in the range specified by aRange; the size of the buffer must therefore be at least the length of the range multiplied by the size of an object reference, as shown in the following example (this is solely for illustration—you should typically not create a buffer simply to iterate over the contents of an array):

NSArray *mArray = // an array with at least six elements...;
id *objects;

NSRange range = NSMakeRange(2, 4);
objects = malloc(sizeof(id) * range.length);

[mArray getObjects:objects range:range];

for (i = 0; i < range.length; i++) {
NSLog(@"objects: %@", objects[i]);
}
free(objects);
NSArray getObjects example.
An alternative to mouviciel's answer is to use NSArray's getObjects:range: method.

id cArray[10];
NSArray *nsArray = [NSArray arrayWithObjects:@"1", @"2" ... @"10", nil];

[nsArray getObjects:cArray range:NSMakeRange(0, 10)];
Or, if you don't know how many objects are in the array at compile-time:

NSUInteger itemCount = [nsArray count];
id *cArray = malloc(itemCount * sizeof(id));

[nsArray getObjects:cArray range:NSMakeRange(0, itemCount)];

... do work with cArray ...

free(cArray);

Example of [NSArray getObjects].
NSArray *someArray = /* .... */;
NSRange copyRange = NSMakeRange(0, [someArray count]);
id *cArray = malloc(sizeof(id *) * copyRange.length);

[someArray getObjects:cArray range:copyRange];

/* use cArray somewhere */

free(cArray);

NSArray getObjects example.
- (id) initWithArray: (NSArray*)array
{
  unsigned    c = [array count];
  id        objects[c];

  [array getObjects: objects];
  self = [self initWithObjects: objects count: c];
  return self;
}

End of NSArray getObjects example article.

Thursday, May 9, 2013

NSDictionary getObjects example ios


getObjects :andKeys:

Returns by reference C arrays of the keys and values in the dictionary.
- (void)getObjects:(id __unsafe_unretained [])objects andKeys:(id __unsafe_unretained[])keys
Parameters
objects
Upon return, contains a C array of the values in the dictionary.
keys
Upon return, contains a C array of the keys in the dictionary.
Discussion of [NSDictionary getObjects]
The elements in the returned arrays are ordered such that the first element in objects is the value for the first key in keys and so on.
Example of [NSDictionary getObjects]
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"A", @"2", @"B", nil];

NSInteger count = [myDictionary count];
id objects[count];
id keys[count];
[myDictionary getObjects:objects andKeys:keys];

for (int i = 0; i < count; i++) {
  id obj = objects[i];
  id key = keys[i];
  NSLog(@"%@ -> %@", obj, key);
}
Example of [NSDictionary getObjects]
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"A", @"2", @"B", nil];

NSInteger count = [myDictionary count];
id __unsafe_unretained objects[count];
id __unsafe_unretained keys[count];
[myDictionary getObjects:objects andKeys:keys];

for (int i = 0; i < count; i++) {
  id obj = objects[i];
  id key = keys[i];
  NSLog(@"%@ -> %@", obj, key);
}