客观C范围问题 [英] Objective C scope problem

查看:119
本文介绍了客观C范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

返回myString;然而,如果我添加代码来更新UIImage,则编译将失败,并且image1未知(未知)。 。 image1是有效的:它在.h中设置,合成并且在该函数下面的方法中,确切的代码行工作
。只有当我将这行代码移动到这个函数时,它才会失败。

  NSString * myfunc(int x)
{
NSString * myString = @MYDATA;
image1.image = [UIImage imageNamed:@image1.png]; //无法编译
返回myString;
}

不应该在这个特定的.m文件中的任何地方识别image1吗?

解决方案

myfunc 是C风格的函数,不是Objective-C方法在你的类范围内,所以你不能看到你的实例变量image1。



你想声明它为一个方法:

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


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?

解决方案

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
{
  ...
} 

这篇关于客观C范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆