Monday, 16 June 2014

Sort array using function

NSInteger sortByDistance(id obj1, id obj2, void *context) {

    NSDictionary *dict1 =(NSDictionary*)obj1;
    NSDictionary *dict2 =(NSDictionary*)obj2;

    NSInteger num1 = [[dict1 objectForKey:@"distance"] integerValue];
    NSInteger num2 = [[dict2 objectForKey:@"distance"] integerValue];

    if (num1 < num2)
        return NSOrderedAscending;
    else if (num1 > num2)
        return NSOrderedDescending;

    return NSOrderedSame;

}


sortedArray = [anArray sortedArrayUsingFunction:sortByDistance context:nil];

reverseSortArray = [[[anArray sortedArrayUsingFunction:sortByDistance context:nil] reverseObjectEnumerator] allObjects];

Sort Array

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[anArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Random number Between limits

-(NSInteger)randNumberBetween:(NSInteger)min and:(NSInteger)max{

    return  (rand() % (max - min) + min);

}

Set Font For Bar Button item

UIBarButtonItem *facebook=[[UIBarButtonItem alloc] initWithTitle:@"FB" style:UIBarButtonItemStylePlain target:self action:@selector(postInFb:)];
[facebook setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                     [UIFont fontWithName:@"icomoon" size:30], NSFontAttributeName,
                                     [UIColor colorWithRed:68.0/255.0 green:85.0/255.0 blue:153.0/255.0 alpha:1], NSForegroundColorAttributeName,
                                     nil]
                           forState:UIControlStateNormal];