stuck on satisfying a warning when dealing with NSOperations

Discussion in 'iOS Programming' started by xnakx, Oct 11, 2008.

  1. xnakx macrumors newbie

    Oct 5, 2008
    thephotoQueue = [[[NSOperationQueue alloc] init] autorelease];
    [thephotoQueue setMaxConcurrentOperationCount:1];
    NSInteger i;
    	for (i = 0; i <=kNumImages -1; i++)
    		NSInvocationOperation* thephotoOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadpropphotos:) object:i]; 
    //Passing argument 3 of  'initwithTarget:selector:object' makes pointer from integer without cast
    		NSLog(@"Queuing new thread");
    		[thephotoQueue addOperation:thephotoOp];
    loadpropphotos takes an int as an argument. everything works fine, but i am trying to clear the warnings out before i hand it over to my testers.
    How would i pass an int to the selector?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You need to use NSNumber to pass an int. NSInvocationOperation is probably sending retain and release on the object parameter, which won't work with an int.
  3. Sbrocket macrumors 65816


    Jun 3, 2007
    i is not an object, which is what that warning tells you - you're attempting to make a pointer from an integer without a cast. While this will compile it will result in less than intended behavior as that method will try to deference the memory located at whatever location it is you're passing it (i=1 would mean it would deference 0x1). That means crashes.

    If you can change the method, make it so that it accepts an NSNumber instead of int and pass [NSNumber numberWithInt:i] instead of just i.

    Otherwise, you can take the slightly more involved route and pass along an int. You'll need to create an NSInvocation and use +[NSInvocationOperation initWithInvocation:]. You can pass along int-typed parameters with NSInvocation using -[NSInvocation setArgument:atIndex:]. Make sure to read the docs for NSInvocation - it can be tricky to use.
  4. xnakx thread starter macrumors newbie

    Oct 5, 2008
    thanks for the info!
    i ended up changing my function to accept a string and dealing with translations there.

Share This Page