Objective C scope problem

送分小仙女□ 提交于 2019-12-25 09:18:11

问题


I have the following Obj C function that works properly:

 NSString* myfunc( int x )
 {
    NSString *myString = @"MYDATA";

    return myString;        
 }

However if I add code to update a UIImage the compile fails with image1 being unknown. image1 is valid: it's set up in the .h, synthesized and that exact line of code works in a method below this function. Only when I move the line of code up to this function does it fail.

 NSString* myfunc( int x )
 {
    NSString *myString = @"MYDATA";
    image1.image = [UIImage imageNamed:@"image1.png"];  // fails to compile
    return myString;        
 }

Shouldn't image1 be recognized anywhere within this particular .m file?


回答1:


myfunc is a C-style function here, not an Objective-C method in your class scope, so you can't see your instance variable image1.

you want to declare it as a method:

- (NSString *)myFuncWithParam:(int)x
{
  ...
} 


来源:https://stackoverflow.com/questions/607357/objective-c-scope-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!