ftp.nice.ch/Attic/openStep/tools/workspace/TheShelf.0.3.3.sd.tgz#/TheShelf.0.3.3.sd/Source/NSArray+MiscExtensions.m

This is NSArray+MiscExtensions.m in view mode; [Download] [Up]

/*************************************************************************
 * File Name: NSArray+MiscExtensions.m
 * Version  : 0.0 alpha
 * Date     : Thu 31-Aug-1997
 *************************************************************************
 *  COPYWHAT (C) 1997 by Tomi Engel
 *
 * This notice may not be removed from this source code.
 * The use and distribution of this software is governed by the
 * terms of the MiscKit license agreement.  Refer to the license
 * document included with the MiscKit distribution for the terms.
 *                    ALL RIGHTS RESERVED
 *
 *************************************************************************
 * Notes      :
 * Bugs       :
 * Author(s)  : tsengel
 * Last update: $Date: 1997/08/31 20:02:57 $
 * History    : $Log: NSArray+MiscExtensions.m,v $
 * History    : Revision 1.0  1997/08/31 20:02:57  tsengel
 * History    : Added to the MiscKit repository
 * History    :
 *************************************************************************/ 

#import "NSArray+MiscExtensions.h"

@implementation NSArray (MiscExtensions)

/*"
    This category provides a number of generally useful convenience methods for the NSArray class cluster.
"*/

- (BOOL)isEmpty
/*"
    Returns YES if there are no objects inside the array. NO otherwise.
"*/
{
    if( [self count] > 0 ) return NO;
    else return YES;
}

- (id)firstObject
/*"
    If there are object in this array this methods returns the #objectAtIndex 0. Returns nil if the array is empty.
"*/
{
    if( [self count] > 0 ) return [self objectAtIndex:0];
    else return nil;
}

- (BOOL)boolAtIndex:(unsigned)index
/*"
    Uses #objectAtIndex: to find the right target and if it responds to the message #boolValue it returns the related value.
    If the object does not respond to that message the value NO is returned (is NO really the best value for a failure ??) 
"*/
{
    id		theValue = [self objectAtIndex:index];

    if( [theValue respondsToSelector:@selector(boolValue)] )
        return [theValue boolValue];
    else return NO;
}

- (char)charAtIndex:(unsigned)index
/*"
    Uses #objectAtIndex: to find the right target and if it responds to the message #charValue it returns the related value.
        If the object does not respond to that message the value 0 is returned (is "0" really the best value for a failure ??)
"*/
{
    id		theValue = [self objectAtIndex:index];

    if( [theValue respondsToSelector:@selector(charValue)] )
        return [theValue charValue];
    else return 0;
}

- (float)floatAtIndex:(unsigned)index
/*"
  Uses #objectAtIndex: to find the right target and if it responds to the message #floatValue it returns the related value.
        If the object does not respond to that message the value 0 is returned (is "0" really the best value for a failure ??)
"*/
{
    id		theValue = [self objectAtIndex:index];

    if( [theValue respondsToSelector:@selector(floatValue)] )
        return [theValue floatValue];
    else return 0;
}

- (double)doubleAtIndex:(unsigned)index
/*"
    Uses #objectAtIndex: to find the right target and if it responds to the message #doubleValue it returns the related value.
    If the object does not respond to that message the value 0 is returned (is "0" really the best value for a failure ??)
"*/
{
    id		theValue = [self objectAtIndex:index];

    if( [theValue respondsToSelector:@selector(doubleValue)] )
        return [theValue doubleValue];
    else return 0;
}

@end



@implementation NSMutableArray (MiscExtensions)

/*"
    This category provides a number of generally useful convenience methods for mutable arrays.
"*/

- (void)addAbsentObject:(id)anObject
/*"
    If the anObject is not part of the array yet (check using the #containsObject method) it is considered absent and
 inserted at the end of the receiver. The object receives a retain message as it's added to the array. If anObject is nil, an NSInvalidArgumentException is raised.
"*/
{
    if( ![self containsObject:anObject] ) [self addObject:anObject];
}

- (void)removeFirstObject
/*"
    Removes the object with the index 0 in the array and sends it a release message. removeFirstObject raises an NSRangeException if there are no objects in the array.
"*/
{
    [self removeObjectAtIndex:0];
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.