views:

150

answers:

3

Hi all,

I am using a singleton backbone in my application to handle accuring errors. They will be handled inside the singleton and broadcast a notification throughout the app when the error has been fixed. Anyways this is not what my question is about but when I pass a new error to the singleton object like this

[[SingletonErrors sharederrors] addError:ErrorDictionary_here];

I want ErrorDictionary_here to be a NSMutableDictionary protected by a given @protocol in my code so whenever I give my code to others in my team they get warnings about error information they might have forgotten to pass into the dictionary.

Is this even possible for starters because this is about adding protocols to setters and a getter is much more easy like

-(NSMutableArray< myprotocol > *)getmyError{

}

I hope some one can help me out.

I'm not seeking for passing objects (read class instances) instead of the dictionary just a protocol applied on my dictionary.

+1  A: 

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
Tim
Let me try that right now .. i thought this was never answered
Johnny Mast
Your @interface is more then correct but i did the following for my code above the declaration of the singleton class.@implementation NSMutableArray (myAddition)- (BOOL)isValid { // Do your validity checking here return YES; // Obviously change this line}@endI did this in order so i could use the function isValid for not only the error single ton but for other cases as well. You where very help full and this question is fully answered.Thank you so much :)
Johnny Mast
You're welcome! If this answered your question completely, you might consider marking it as "accepted" so that others can use it in their own programming. Just click the checkmark at the top of the answer.
Tim
Okey i have ont that now .. this is officialy a finished subject.
Johnny Mast
+1  A: 

It is also possible to implement a protocol through a category like so:

@interface NSMutableDictionary_TD(ErrorExtensions) <ErrorProtocol>
@end

@implementation NSMutableDictionary(ErrorExtensions)
//implement the ErrorProtocol here
@end
Tom Dalling
+1  A: 

Thats correct but the doesnt feel nice to me .. my solution merged with tim`s was

@implementation NSMutableArray (myAddition)

  • (BOOL)isValid { // Do your validity checking here

    return YES; // Obviously change this line } @end

This saves a load of code .. Im a Objective C in blood and fains .. less is better :) .. thanks for your reply anyways because im sure this issue is not a basic objc issue. Its more advanced and i think loads of people will find this topic and see the fix and you fix is 100% right as well so thanks for that !..

My heart is to small to store the loving replies i get here :).

Johnny Mast