2013-06-24 20 views
0

我會通過對象通過互聯網對象來獲取數據,我的申請,我想,每次該數據已更新或任何對象添加正在使用這個類就得到通知,該數據一直是當前類更新。目前,我在這裏達到iOS - 在NSMutableArray中獲取數據插入通知?

#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 
+2

通過做[KVO在NSMutableArray](http://stackoverflow.com/questions/302365/observing-an-nsmutablearray-for-insertion-removal)你可以觀察一個特定的可變用於插入或刪除元素的數組實例。 – Amar

+1

退房http://streetsaheadllc.com/article/key-value-observing-nsarray-and-nsdictionary – iCoder

回答

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 

在課堂上。

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

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

這可能是更好地使用通知(如上所述),或創建爲類做所述網絡請求,並且當數據是在一個委託;更新數組然後發送消息。我不認爲這是必要的/邊緣的壞設計來教這個陣列。它打破了一個類設計的內聚原理

+0

樁和的addObserver方法不完全完成,但我注意到,這是非常接近的僞代碼。自動完成應該爲您完成 –