views:

95

answers:

3
NSString *temp = [self randomBallPick:temp];

the following error: Pass-by-value argument in message expression is undefined

why?

-(NSString*) randomBallPick:(NSString*) oneFilename
{

    //create an array of filenames
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    for (int c=0;c<37;c++)
    {
     NSString *imageName = [NSString stringWithFormat:@"ball_%d.png", c];
     [imageArray addObject: imageName];
    }

    //pick one filename
    int numFileNames = [imageArray count];
    int chosen = arc4random() % numFileNames;
    oneFilename = [imageArray objectAtIndex: chosen];
    [imageArray release];
    return oneFilename;
}
A: 

I suspect that your method expects a pointer and you are passing data by value.

Make sure you have temp defined before the line where you pass it to your method.

Do you define temp before this line? In C (not sure about objective-c) if compiler does not see definition of variable before variable is used it defaults the type of variable to int so it calls randomBallPick:(int) instead of randomBallPick:(NSString*).


It would help if you show us the function signature of randomBallPick: and the definition of temp.

Does your method look like this: randomBallPick:(NSString*)temp , assuming temp is NSString* ? Is temp initialized properly?

Additionally, assuming you already have temp defined before this line, you are redefining temp again ...

stefanB
+2  A: 

You're not using the string you're passing in to do anything. Seems like you needn't pass it in but should initialise it inside the method, set it, autoRelease and return it

You're defining temp on that line so the compiler is satistifed that temp exists but when it's only initialised so it's not a valid pointer yet.

Hope that makes sense. This is my educated guess at what's happening. Happy to be corrected by an objective-c guru.

Tom Duckering
+1  A: 

As far as I can tell you're creating an array of 37 file names and then return one of them randomly.

I think the following provides the same functionality:

- (NSString*)randomBallPick
{
    [NSString stringWithFormat:@"ball_%d.png", arc4random() % 37];
}

If you want to keep the array around and reuse it, I'd do something like this:

- (NSString*)randomBallPick
{
    static NSMutableArray *imageArray;

    if (!imageArray) {
        imageArray = [[NSMutableArray alloc] init];
        for (int c = 0; c < 37; c++)
        {
            NSString *imageName = [NSString stringWithFormat:@"ball_%d.png", c];
            [imageArray addObject:imageName];
        }
    }

    //pick one filename
    NSUInteger numFileNames = [imageArray count];
    NSUInteger chosen = arc4random() % numFileNames;
    return [imageArray objectAtIndex:chosen];
}
Thomas Müller
thank you! btw i'm trying in another post to add a loop that maintain a list of not yet picked balls in this array (for unique purpose) and pick it as the return, any idea how to implement in your second function (the reuse one) ?
omri
I added an answer to your other question. HTH.
Thomas Müller
i posted you there.
omri