Showing posts with label initWithArray. Show all posts
Showing posts with label initWithArray. Show all posts

Sunday, June 16, 2013

NSCountedSet initWithArray example in Objective C (iOS).


NSCountedSet initWithArray

Returns a counted set object initialized with the contents of a given array.

- (id)initWithArray:(NSArray *)anArray

Parameters
anArray
An array of objects to add to the new set.

Return Value of [NSCountedSet initWithArray]
An initialized counted set object with the contents of anArray. The returned object might be different than the original receiver.

NSCountedSet initWithArray example.
- (void)testOccurrenceCounting
{
    NSArray *numbers = @[@1, @3, @4, @6, @6, @3, @1, @3];
    NSCountedSet *set = [[NSCountedSet alloc] initWithArray:numbers];
    NSMutableArray *counters = [NSMutableArray arrayWithCapacity:[set count]];
    [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
        [counters addObject:[[Pair alloc] initWithKey:obj value:@([set countForObject:obj])]];
    }];
    [counters sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES]]];

    NSLog(@"%@", counters);
}

Example of [NSCountedSet initWithArray].
NSMutableArray *array = …;
NSCountedSet *countedSet = [[[NSCountedSet alloc] initWithArray:array]
    autorelease];

// Array with distinct elements only, sorted by their repeat count
NSArray *distinctArray = [[countedSet allObjects]
    sortedArrayUsingFunction:countedSort context:countedSet];

// Array with all the elements, where elements representing the same
// object are contiguous
NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:[array count]];
for (id object in distinctArray) {
    for (NSUInteger i = 0; i < [countedSet countForObject:object]; i++) {
        [sortedArray addObject:object];
    }
}

NSCountedSet initWithArray example.
countedList = [[NSCountedSet alloc] initWithArray:listOfItems];//listOfItems is an array of country name strings with duplicates
    listOfItems2 = [NSMutableArray array]; // array without duplicates that's the data source for the UITableView

    for (NSString *aCountry in countedList) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:aCountry forKey:@"name"];
        [dict setValue:[NSString stringWithFormat:@"%lu",[countedList countForObject:aCountry]] forKey:@"count"];
        [listOfItems2 addObject:dict];
    }

End of NSCountedSet initWithArray example article.

Friday, May 31, 2013

NSArray initWithArray example in Objective C (iOS).


NSArray initWithArray

Initializes a newly allocated array by placing in it the objects contained in a given array.

- (id)initWithArray:(NSArray *)anArray

Parameters of [NSArray initWithArray]
anArray
An array.

Return Value
An array initialized to contain the objects in anArray. The returned object might be different than the original receiver.

Discussion of [NSArray initWithArray]
After an immutable array has been initialized in this way, it cannot be modified.

NSArray initWithArray example.
NSMutableArray * arr1 = [[NSMutableArray alloc]initWithObjects:@"1", @"2", nil];
NSMutableArray * arr2 = [[[NSMutableArray alloc] initWithArray:arr1 copyItems:YES] autorelease];
NSString * no = [arr2 objectAtIndex:0];
NSLog(@"%@", no);

Example of [NSArray initWithArray].
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
NSMutableArray arr2 = [[[NSArray alloc] initWithArray: arr1 copyItems:YES] mutableCopy];

NSArray initWithArray example.

-(void)createButtons {
    for (int i=0; i<8 data-blogger-escaped-br="" data-blogger-escaped-i="" data-blogger-escaped-nbsp="">         for (int j=0; j<8 data-blogger-escaped-br="" data-blogger-escaped-j="" data-blogger-escaped-nbsp="">            
            UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
            but.frame = CGRectMake(i*60, j*60, 60, 60);
          
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but setTitle:@\"text\" forState:UIControlStateNormal];
            [self.view addSubview:but];
           
            [but addTarget:self action:@selector(pressLetter:) forControlEvents:UIControlEventTouchUpInside];
           
            [tempTiles insertObject:but atIndex:(i*8)+j];
           
        }
    }
   
    // tiles is declared as an instance variable of class
    tiles = [[NSArray alloc] initWithArray:tempTiles]; // INSTRUMENTS POINTS TO THIS LINE FOR LEAK
   
    [tempTiles release];

}

End of NSArray initWithArray example article.