开发者

How To Pass a Dictionary To a Function

Alright, so I think I'm doing this the right way. I'm new to objective-C, so I'm not sure about the syntax... I have a set of code that I need to call multiple times, from different files. So I made a new class that has a method in it that I'll call and pass it the values that it needs.

Because I am passing different values I've put them in a dictionary and decided to just pass the dictionary. Here is that code:

NSNumber *testNum = [NSNumber numberWithInt:varMoney];


    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@"OMG, Object 1!!!!" forKey:@"1"];
    [dic开发者_如何学Ctionary setObject:@"Number two!" forKey:@"2"];
    [dictionary setObject:testNum forKey:@"3"];

This code creates a test variable, and then puts it into the dictionary "dictionary." That all works, I have my nice little dictionary. However, now I need to create the class and it's method that will recieve the dictionary, and do something with it.

This is my class header file:

#import <UIKit/UIKit.h>


@interface EndOfTurnObjC : UIView {

}

@end

And this is the implementation file:

#import "EndOfTurnObjC.h"


@implementation EndOfTurnObjC


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}



- (void)dealloc {
    [super dealloc];
}


@end

I haven't created any of the real code, because I'm not sure how to do the passing. I need to create a function (Method?) in the class that will take a Dictionary has a parameter, and then return the dictionary.

I also have no idea how to call such a function because it's in the class. So, the questions are: 1: How do I define the method in the class to accept the dictionary as a parameter (and then perhaps some example code to pull out one of the objects in a dictionary, so I can be sure it works)

2: How do I return the dictionary at the end of the method?

3: How do I call this method, in the class, from another class? (I know it involves making an object of thing class and calling the method of the object... I think, but I'm not sure about the syntax.)

Please include relavent code for the 3 files (header, implementation, and the other class that I call from). Thank you so much, I've been working on this particular problem for a while now.


Apple's The Objective-C Programming Language is a good and pretty concise reference for Objective-C syntax. What you want is just a normal method that takes an NSDictionary as a parameter. So as given in that document:

A message with a single argument affixes a colon (:) to the selector name and puts the argument right after the colon. This construct is called a keyword; a keyword ends with a colon, and an argument follows the colon, as shown in this example:

[myRectangle setWidth:20.0];

So a method call to pass dictionary would look like:

[someObject setAttributes:dictionary];


In the header:

-(NSMutableDictionary *) doSomethingWithDictionary:(NSMutableDictionary *) aDict;

in the implementation:

-(NSMutableDictionary *) doSomethingWithDictionary:(NSMutableDictionary *) aDict{
    //do something with the dictionary 
    return aDict;
}

To call the method:

NSMutableDictionary *returnDict=[EndOfTurnObjC doSomethingWithDictionary:dictionary];

Note that as a matter of good design you wouldn't want to pass a mutable dictionary around like a token. That is asking for trouble. Instead pass static dictionaries and get another dictionary back.

You also shouldn't be passing data to a UIView. Instead, your UIViewController should process the data and then populate the view's UI elements as needed.


if you just want to do stuff to your dictionary u just

-(void) changeMyDictionary:(NSMutableDictionary * ) dictionary_

{

[dictionary_ doStuff];
    ....
    ...

}

no need to return the dictionary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜