Friday, May 31, 2013

NSArray containsObject example in Objective C (iOS).


NSArray containsObject

Returns a Boolean value that indicates whether a given object is present in the array.

- (BOOL)containsObject:(id)anObject

Parameters of [NSArray containsObject]
anObject
An object.

Return Value
YES if anObject is present in the array, otherwise NO.

Discussion of [NSArray containsObject]
This method determines whether anObject is present in the array by sending an isEqual: message to each of the array’s objects (and passing anObject as the parameter to each isEqual: message).

NSArray containsObject example.
if ([array containsObject:[NSString stringWithFormat:@"%d", indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}

Example of [NSArray containsObject].
- (void) setupDataSource {
        NSMutableArray *dataSourceArray = [NSMutableArray array];
        NSArray *eths = [NSArray arrayWithObjects:@"africanamerican",
@"asian", @"caucasion",
                                         @"easternindian", @"hispanic", @"nativeamerican", @"other",
nil];
        selectedEthnicity = [[NSMutableArray alloc] initWithArray:[defaults
arrayForKey:USERDEFAULT_ETHNICITIES]];
        NSString *notImgUrl = @"bundle://NotSelected.png";
        NSString *selImgUrl = @"bundle://IsSelected.png";
        for(NSString *e in eths){
                NSString *text = nil;
                NSString *imageUrl = notImgUrl;
                NSString *url = nil;
                if([e isEqualToString:@"africanamerican"]){
                        text = @"African American";
                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"asian"]){
                        text = @"Asian";

                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"caucasion"]){
                        text = @"Caucasion";
                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"easternindian"]){
                        text = @"Eastern Indian";

                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"hispanic"]){
                        text = @"Hispanic";
                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"nativeamerican"]){
                        text = @"Native American";

                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }else if([e isEqualToString:@"other"]){
                        text = @"Other";

                        if([selectedEthnicity containsObject:e])
                                imageUrl = selImgUrl;
                        url = [NSString stringWithFormat:@"cs://multi/select?val=%@", e];
                }
                [dataSourceArray addObject:[TTTableRightImageItem itemWithText:text
                                                                                                                          imageURL:imageUrl
                                                                                                                                   URL:url]];
        }

        self.dataSource = [CSSectionedDataSource
dataSourceWithArrays:@"Ethnicities", dataSourceArray, nil];
}


NSArray containsObject example.
- (void)addObject : (id)object {

    if(![objectsToArchive containsObject:object]) {
        [objectsToArchive addObject:object];
    }
}
End of NSArray containsObject example article.