Problems using a variable with a method

Discussion in 'iOS Programming' started by q582gmzhi, Apr 8, 2014.

  1. q582gmzhi, Apr 8, 2014
    Last edited by a moderator: Apr 8, 2014

    q582gmzhi macrumors newbie

    Joined:
    Mar 20, 2014
    #1
    Hi,

    I am having problems using a string variable within a method statement. The code works fine no issues if i use it a a stand alone method, but i have 6 methods using the same code the only differences are an int value which i am ok with and the 'myImage' name or string value which is the bit i cant seem to work out.

    This part of the code works fine as a standalone method:
    Code:
    [[[self myImage] layer] setValue:imageRotation.toValue forKey:imageRotation.keyPath];
    So i am trying to use one method and pass variables to it. But when i pass the parameters of v_myImageName as a string it keeps coming up with the error 'No visible interface for ViewController declares the selector v_myImageName. I think it cannot see the variable within the v_myImageName and is looking for an image / layer called v_myImageName.

    Code:
    -(void) testMethod: (int)v_Number myImageName: (NSString *)v_myImageName
    {
    //other code
    [[[self v_myImageName] layer] setValue:imageRotation.toValue forKey:imageRotation.keyPath];
    //other code
    }
    Any advice will be welcomed.

    Thanks in advance.

    Daz...
     
  2. q582gmzhi thread starter macrumors newbie

    Joined:
    Mar 20, 2014
    #2
    I have figured out the error message "No visible interface for ViewController declares the selector v_myImageName" i needed to add it to the .h file.

    The code now runs with no error messages but is still not working as i had hoped so I have added breakpoints and stepped through the code and watched the variable being passed ok but the variable value is not visible within the section [[[self v_myImageName] layer]. But is visible outside this section.

    For example if i passed the string "green_image" in the variable v_myImageName i hoped it would translate to [[[self green_image] layer].

    But this does not work.

    Any ideas what i am doing wrong?

    Daz...
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    I'm going to teach you to fish.

    When you nest bracketed method calls, like you've done on you one line, it obscures what's really going on. In order to understand better what's really going on I suggest that you change your single expression with three sets of nested method calls into three lines of code. The first line will store the result of [self myImage], the second will store the result of [resultOfLineOne layer]. And the third line will store the result of the setValue:forKey: method call. Once you do that you just have to replace the first line with the value of a parameter from your method call.

    Let's see what you catch.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Methods use selectors, not strings.

    Look into getting selectors from strings and invoking them.
     
  5. DannyBres macrumors 65816

    DannyBres

    Joined:
    Oct 30, 2007
    Location:
    UK
    #5
    You are sending the message v_myImageName to self. As you said self does not have the method implemented. I do not know why you are create another NSString object and passing a pointer to that in your method, you might as well send a pointer to the actual object.

    Assuming v_myImageName is the name of a UIView (or a subclass of it), can you not just pass the pointer of the UIView (or a subclass of it), in your method. so you would have something like.

    -(void) testMethod: (int)v_Number myImage: (UIView *)v_myImage
    {
    //other code
    [[v_myImage layer] setValue:imageRotation.toValue forKey:imageRotation.keyPath];
    //other code
    }

    then to use it just use the following:

    [self testMethod:??? myImage:green_image];

    Also I hate all the underscores, use CamelCase. :)
     
  6. q582gmzhi, Apr 9, 2014
    Last edited: Apr 11, 2014

    q582gmzhi thread starter macrumors newbie

    Joined:
    Mar 20, 2014
    #6
    I have had a try with DannyBres example but getting error of local declaration of 'v_myImageName' hides instance variable. I have copied my full code below and will have a look at other suggestions in meantime.

    Thanks

    Daz.....

    Code:
        UIView *HEADING_DI;
        [self testMethod:x_aircraftheading myImage:HEADING_DI];
    }
    
    //=======================================================
    -(void) testMethod:(int)v_Number myImage:(UIView *)v_myImageName
        {
    //some code
            //================================================================================
            [[v_myImageName layer] setValue:imageRotation.toValue forKey:imageRotation.keyPath];
            [[v_myImageName layer] addAnimation:imageRotation forKey:@"imageRotation"];
            //================================================================================
    
    }
     
  7. PBG4 Dude macrumors 68020

    PBG4 Dude

    Joined:
    Jul 6, 2007
    #7
    You have a variable with the name 'v_myImageName' that has greater scope than a single method.

    Maybe you declared a @property with this name?

    Try changing the local variable from 'v_myImageName' to something else and see if that solves your problem.

    If so, you'll need to track down where else you are using 'v_myImageName' in your class.
     
  8. DannyBres macrumors 65816

    DannyBres

    Joined:
    Oct 30, 2007
    Location:
    UK
    #8
    Should it be?

    [self testMethod:x_aircraftheading myImage:[self HEADING_DI]];
     
  9. q582gmzhi, Apr 9, 2014
    Last edited: Apr 11, 2014

    q582gmzhi thread starter macrumors newbie

    Joined:
    Mar 20, 2014
    #9
    I had a look at ArtOfWarFare suggestion and managed to get it working but with an warning about 'performSelector may cause a memory leak because its selector is unknown' using the following, solved one problem and created another :)
    Daz..

    Code:
    -(void) testMethod:(int)v_Number myImage:(NSString *)v_myImageName
        {
            //some code
            SEL aSel=NSSelectorFromString(v_myImageName);
    //===========================================================================================
            [[[self performSelector:(aSel)] layer] setValue:imageRotation.toValue forKey:imageRotation.keyPath];
            [[[self performSelector:(aSel)] layer] addAnimation:imageRotation forKey:@"imageRotation"];
            //============================================================================================
     
  10. PBG4 Dude macrumors 68020

    PBG4 Dude

    Joined:
    Jul 6, 2007
    #10
    Usually, perform selector: methods get an argument like so:
    @selector(aSel)

    You have just (aSel) in your code above.
    (i.e. [self performSelector:mad:selector(aSel) ... instead of what you have)
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    I guess you didn't want to learn to fish. You just want to eat fish sticks with ketchup.

    Don't use performSelector() for this task. Your code in message #6 is correct. You need to fix the error message from that code.

    You need to learn to code in a different way. Copy and paste of code and then coming here to ask why it doesn't work won't work for very long.
     
  12. q582gmzhi thread starter macrumors newbie

    Joined:
    Mar 20, 2014
    #12
    PhoneyDeveloper thanks for your input.

    I did have a go with your suggestion without success, so i tried another suggestion. Unfortunatley some people like me are very slow learners and not as bright or as clever as some. Thats why i make signs for a living and why i do not write software for a living, but i do find it interesting putting bits of code together and trying to create things. I will take another look at #6 and have a go at trying to get it to work.

    Many thanks

    Daz...
     
  13. q582gmzhi thread starter macrumors newbie

    Joined:
    Mar 20, 2014
    #13
    I have managed to get #6 working. Thanks everyone for their input and suggestions.

    Daz....
     

Share This Page