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;
}
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.
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];
}
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 ...
精彩评论