Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

q582gmzhi

macrumors newbie
Original poster
Mar 20, 2014
12
0
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...
 
Last edited by a moderator:
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...
 
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.
 
Methods use selectors, not strings.

Look into getting selectors from strings and invoking them.
 
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. :)
 
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"];
        //================================================================================

}
 
Last edited:
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.
 
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"];
        //============================================================================================
 
Last edited:
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)
 
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.
 
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...
 
I have managed to get #6 working. Thanks everyone for their input and suggestions.

Daz....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.