NSDictionary Pass Between Classes
I'm trying to pass a NSMutableDictionary object between two classes. I've tried adding a method that returns a dictionary to no avail.
-(NSMutableDictionary*) getResponse
{
return response; //where response is a dictionary that has key开发者_运维百科s and objects assigned
}
but even then the dictionary can not be pulled from the other class; it returns "null."
I've tried adding a method that returns itself to no avail.
-(NSMutableDictionary*) getResponse { return response; }
That's because this method returns a dictionary. For the method to return itself, it would have to ask the object self
for its implementation for the method's selector, or ask the Objective-C runtime to return the Method
structure.
But that's not what you actually want to do, is it?
If you mean for this method to return the response dictionary, well, that's exactly what it does. It is correct as written (aside from its name, as I already explained in my comment on the question).
… but even then the data can not be pulled from the other class;
The method does not claim to return a data object; it claims to return a dictionary object. If it did return a data object, that would be a bug.
… it returns void.
No, it returns NSMutableDictionary *
. void
is a type; it makes sense in no other context.
If you meant it returns nil
(the pointer to no object), then that means you didn't have a response dictionary to return. Don't ask this object for the response until it has one.
You may want to make the object that wants the response the delegate of the object that holds the response, and have the response-holder send a message to its delegate when the response comes in. You may even include the response in that message; the alternative would be that the response-holder passes only itself, and the delegate asks the response-holder for the response dictionary in its implementation of the delegate method.
More information:
- Object-Oriented Programming with Objective-C
- The Objective-C Programming Language
- Cocoa Fundamentals Guide (including the explanation of delegation, but don't skip to that part)
If your method returns nil, it's because your response
variable is nil. It's likely not initialized at this point. Try doing NSLog(@"Response: %@", response);
within the getResponse method. See if the console prints (null), or a valid NSMutableDictionary object.
精彩评论