[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to set value in NSArray
From: |
Richard Frith-Macdonald |
Subject: |
Re: How to set value in NSArray |
Date: |
Tue, 13 Jul 2004 17:24:31 +0100 |
On 13 Jul 2004, at 17:09, Vikram Vyas wrote:
Hello,
I would like to set value of an NSArray object, monthlyRain, to a
particular value
-(void)setMonth:(NSNumber *)month AmountOfRain:(NSNumber *)rain
{
if([monthlyRain objectAtIndex:[month unsignedIntValue]] != rain)
{
// set monthly value for the month given by the index to the
amount
// rain
}
}
but I cannot find an appropriate method. Would appreciate any help.
You can't ... and NSArray is an immutable object.
What you can do is make a mutable copy of the object and use
the replaceObjectAtIndex:withObject: method of NSMutableArray.
eg.
@interface MyClass : NSObject
{
NSMutableArray * monthlyRain;
}
- (id) initWithArray: (NSArray*)anArray;
- (void) setMonth: (NSNumber*)month amountOfRain: (NSNumber*)rain;
@end
@implementation MyClass
- (void) dealloc
{
DESTROY(monthlyRain);
[super dealloc];
}
- (id) initWithArray: (NSArray*)anArray
{
monthlyRain = [anArray mutableCopy];
return self;
}
- (void) setMonth: (NSNumber*)month amountOfRain: (NSNumber*)rain
{
unsigned index = [month unsignedIntValue];
if ([[monthlyRain objectAtIndex: index] isEqual: rain] == NO)
{
[monthlyRain replaceObjectAtIndex: index withObject: rain];
}
}