If I understand what you're asking, you should be able to do this without too much hassle. In your singleton class SingletonErrors, you should have:
@interface SingletonErrors : NSObject {
// some definitions ...
// The current array of all errors. This can also be an NSMutableSet if you like
NSMutableArray *sharedErrors;
// more definitions ...
}
// some properties ...
@property(nonatomic,retain) NSMutableDictionary<ErrorProtocol> *sharedErrors;
// more properties ...
- (void)addError:(NSMutableDictionary<ErrorProtocol> *)newError;
@end
You should create the protocol to be implemented. In this sample protocol, let's say you want to provide a single method to check whether the object is valid - that is, the dictionary contains all the relevant information.
@protocol ErrorProtocol
- (BOOL)isValid;
@end
You'll then need to subclass NSMutableDictionary so that your class implements the ErrorProtocol protocol:
@interface MyMutableDictionary : NSMutableDictionary <ErrorProtocol> {
}
@end
@implementation MyMutableDictionary
- (BOOL)isValid {
// Do your validity checking here
return YES; // Obviously change this line
}
@end
Then, whenever you throw an error, you can pass in a new instance of MyMutableDictionary to SingletonErrors, and have it call the isValid selector on the MyMutableDictionary, since it's assured that the dictionary will conform to ErrorProtocol and responds to isValid:
- (void)addError:(NSMutableDictionary<ErrorProtocol> *)newError {
if([newError isValid]) {
// Add the new error to the current array of errors
[self.sharedErrors addObject:newError];
// Other code to "broadcast" the error would go here
} else {
// Some code to error out of adding the error would go here
}
}
Overall, what this solution does is:
- Hold a NSMutableArray of all errors in SingletonErrors
- Each error is an NSMutableDictionary that conforms to ErrorProtocol
- The object we use for each error is MyMutableDictionary, a subclass of NSMutableDictionary
- The protocol ErrorProtocol defines a method
isValid that checks whether the error is OK to be added
- The SingletonErrors object calls the
isValid method and adds the error appropriately