1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*
NSArrayAdditions.m
Implementation of the additions to the NSArray class for the
ProjectManager application.
Copyright (C) 2005 Saso Kiselkov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "NSArrayAdditions.h"
#import "NSSetAdditions.h"
#import "NSDictionaryAdditions.h"
@implementation NSArray (PMAdditions)
/**
* Creates a mutable autoreleased copy of the receiver, but instead
* of just making this top-level object mutable, it traverses through
* all objects in the receiver and recursively replaces any occurence
* of an array, dictionary or set with their mutable equivalents.
*
* @array A mutable copy of the receiver with any contained sets,
* dictionaries and arrays recursively replaced with their
* mutable equivalents.
*/
- makeDeeplyMutableEquivalent
{
id (*ObjectAtIndex)(id, SEL, unsigned int);
void (*AddObject)(id, SEL, id);
NSMutableArray * mutableCopy;
unsigned int i, n;
Class arrayClass = [NSArray class],
dictionaryClass = [NSDictionary class],
setClass = [NSSet class];
n = [self count];
mutableCopy = [NSMutableArray arrayWithCapacity: n];
ObjectAtIndex = (id (*)(id, SEL, unsigned int)) [self methodForSelector:
@selector(objectAtIndex:)];
AddObject = (void (*)(id, SEL, id)) [mutableCopy methodForSelector:
@selector(addObject:)];
for (i = 0; i < n; i++)
{
id object;
object = ObjectAtIndex(self, @selector(objectAtIndex:), i);
if ([object isKindOfClass: arrayClass] ||
[object isKindOfClass: dictionaryClass] ||
[object isKindOfClass: setClass])
{
AddObject(mutableCopy, @selector(addObject:), [object
makeDeeplyMutableEquivalent]);
}
else
{
AddObject(mutableCopy, @selector(addObject:), object);
}
}
return mutableCopy;
}
@end
|