I have two properties NSString, both of which I have synthesized and are readonly and so I cannot use the property method self. but I though both the passed string most be retained. So, I added retain to retain the properties. But I feel, I will have leaks here since the passed objects are have increased their retain count. But will my properties retain these string without sending message retain.
-(void)setValue:(NSString *)passedString1 second:(NSString *)passedString2{
myString = [passedString1 retain];
hisString = [passedString2 retain];
}
Lets say I have property for array(variable) declared as NSArray and I pass NSDictionary as argument this way;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"text", @"This is some text", nil];
[self setValueForArray:dict];
-(void)setValueForArray:(NSDictionary*)passedDict{
NSArray *someArray = [NSArray arrayWithObject:passedDict];
if(array!=someArray){
[array release];
array = someArray; //I dont think I should retain this property here since it is retained by someArray
}
}
Is this approach correct for NSDictionary and NSArray types.
Use
copyfor Strings and release the previous object:If you need to
releasethe Strings that were passed in as parameters, do it in the calling method after callingsetValue: