Showing posts with label addObject. Show all posts
Showing posts with label addObject. Show all posts

Friday, June 21, 2013

NSMutableSet addObject example in Objective C (iOS).


NSMutableSet addObject

Adds a given object to the set, if it is not already a member.

- (void)addObject:(id)object

Parameters of [NSMutableSet addObject]
object
The object to add to the set.

NSMutableSet addObject example.
- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    NSSet *s=[NSSet setWithObject:@"setWithObject"];
    NSMutableSet *m=[NSMutableSet setWithCapacity:1];
    [m addObject:@"Added String"];
    NSMutableSet *n = [[NSMutableSet alloc] initWithCapacity:1];
    [self showSuperClasses:s];
    [self showSuperClasses:m];
    [self showSuperClasses:n];
    [self showSuperClasses:@"Steve"];  
}

- (void) showSuperClasses:(id) anObject{
    Class cl = [anObject class];
    NSString *classDescription = [cl description];
    while ([cl superclass])
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }
    NSLog(@"%@ classes=%@",[anObject class], classDescription);
}

Example of [NSMutableSet addObject].
At some point before you use the set, you need to create a new NSMutableSet.

To make it easy, you can use something like the following to automatically allocate a new mutable set when you ask to use it for the first time.

- (NSMutableSet *)NHPList {
    if (NHPList == nil) {
        NHPList = [[NSMutableSet alloc] init];
    }
    return NHPList;
}
You would also have to release the memory, usually in your viewDidUnload method by setting NHPList to nil.

If this is the only place that you set the data, you could also just change this line:

[NHPList addObject:[sub name]];
to:

if (self.NHPList == nil) {
    self.NHPList = [[NSMutableSet alloc] init];
}
[self.NHPList addObject:[sub name]];

NSMutableSet addObject example.
+ (NSSet *)variablesInExpression:(id)anExpression
{
NSMutableSet *setOfVariables = [[NSMutableSet alloc] init];
for (NSString *str in anExpression) {
    if ([str hasPrefix:VARIABLE_PREFIX]) {
        [setOfVariables addObject:str];
    }
}
[setOfVariables autorelease];
return setOfVariables;
}

End of NSMutableSet addObject example article.

Sunday, June 16, 2013

NSCountedSet addObject example in Objective C (iOS).


NSCountedSet addObject

Adds a given object to the set.

- (void)addObject:(id)anObject

Parameters
anObject
The object to add to the set.

Discussion of [NSCountedSet addObject]
If anObject is already a member, addObject: increments the count associated with the object. If anObject is not already a member, it is sent a retain message.

NSCountedSet addObject 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];
    }

Example of [NSCountedSet addObject].
    NSArray *arr = @[@1, @3, @4, @6, @6, @3, @1, @3];
    NSCountedSet *totalSet = [NSCountedSet setWithArray:arr];
    NSMutableArray *dictArray = [NSMutableArray array];
    for (NSNumber *num in totalSet) {
        NSDictionary *dict = @{@"number":num, @"count":@([totalSet countForObject:num])};
        [dictArray addObject:dict];
    }
    NSArray *final = [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES ]]];
    NSLog(@"%@",final);

NSCountedSet addObject example.
@interface NSCountedSet (JRCountedSetAdditions)

- (NSArray *) objectsWithCount:(NSUInteger) count;

@end

@implementation NSCountedSet (JRCountedSetAdditions)

- (NSArray *) objectsWithCount:(NSUInteger) count {
   NSMutableArray *array = [NSMutableArray array];
   for(id obj in self) {
      if([self countForObject:obj] == count) {
        [array addObject:obj];
      } 
   }
   return [array copy];
}

@end

End of NSCountedSet addObject example article.