Jump to content


Forumul s-a mutat!

Aceasta este arhiva forumului MacForum.ro din perioada 2010-2014.
Noul forum poate fi accesat aici.
Conturile vechi nu mai sunt valabile pe noul forum!

Photo

intrebare deep copy


  • Please log in to reply
No replies to this topic

#1 boboc

boboc

    Veteran

  • Membri
  • PipPipPipPip
  • 668 posts
  • Locationvalcea

Posted 20 July 2010 - 22:28

Am si eu o intrebare despre deep copy,am urmatoarea categorie ce adauga o metoda clasei NSDictionary,este un exemplu dintr-o carte.
Este un dictionar ce contine mai multe array-uri care contin la randul lor NSString-uri.
Din cate stiu eu copiile NSDictionary NSArray si NSSet sunt shallow by default.

@implementation NSDictionary (MutableDeepCopy)

- (NSMutableDictionary *) mutableDeepCopy
{
	NSMutableDictionary *ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
	
	NSArray *keys = [self allKeys]; 
	
	for (id key in keys)
	{
		id oneValue = [self valueForKey:key];
		id oneCopy = nil;

		if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]) 
			oneCopy = [oneValue mutableDeepCopy];
		else if ([oneValue respondsToSelector:@selector(mutableCopy)]) 
			oneCopy = [oneValue mutableCopy];
		if (oneCopy == nil) 
			oneCopy = [oneValue copy];

		[ret setValue:oneCopy forKey:key]; return ret;
	}
	return ret;
}
@end

Nu inteleg cum se realizeaza deep copy in codul de mai sus.
O metoda pentru deep copy care o stiu ar fi prin NSKeyedArchiver/NSKeyedUnarchiver:

array2 = [NSKeyedUnarchiver unarchiveObjectWithData: [NSKeyedArchiver archiveDataWithRootObject: array1];





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users