0

我将通过 Internet对象逐个对象获取我的应用程序的数据,我希望每次更新数据或添加任何对象时,使用该类的当前类都会收到数据已更新的通知。目前我已经到达这里

#import <Foundation/Foundation.h>

@interface MSMutableArray : NSMutableArray
- (void) addObjectInArray:(id)anObject;
@end

然后是实现类

#import "MSMutableArray.h"

@implementation MSMutableArray
- (void) addObjectInArray:(id)anObject
{
    [self addObject:anObject];
//  Notify the current Class that an element has been added????
}
@end
4

1 回答 1

1
#import "MSMutableArray.h"

@implementation MSMutableArray
- (void) addObjectInArray:(id)anObject
{
    [self addObject:anObject];
//  Notify the current Class that an element has been added
[[NSNotificationCenter defaultCenter]postNotification:@"arrayUpdated withObject:nil]
}
@end

In the class using the array.

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(myMethod):...

-(void)myMethod{
//do stuff here
}

It may be better to use notification (as explained above) or create a delegate for the class doing the network request and when the data comes in; update the array and then send the message. I don't think it is necessary /borderline bad design to teach the array to do this. It breaks the cohesion principle of a class design

于 2013-06-24T10:41:29.367 回答