Objective-C:无法更改函数内实例变量的值
我无法更改函数内实例变量的值。
我定义了一个类:
// info.h
#import <Foundation/Foundation.h>
@interface NSMyObject : NSObject
{
NSInteger i;
}
-(void) setI:(NSInteger)v;
@end
#import "info.h"
@implementation NSMyObject
-(void) setI:(NSInteger)v ;
{
i=v;
}
- (void)dealloc {
[super dealloc];
}
@end
我使用参数 temObj 调用函数“myFunction”,它是一个 NSMyObject 实例。
myFunction(temObj);//temObj is NSMyObject
在函数中,我可以更改参数obj
的实例变量的值。
-(void)myFunction:(NSMyObject*) obj;
{
[obj setI:0];
}
...期望这会改变 temObj 的内容。
但是当我检查函数 myFunction
中 obj
的操作结果时,temObj.i
的值没有改变。
欢迎大家评论
谢谢
I cannot change the value of an instance variable inside a function.
I have defined a class:
// info.h
#import <Foundation/Foundation.h>
@interface NSMyObject : NSObject
{
NSInteger i;
}
-(void) setI:(NSInteger)v;
@end
#import "info.h"
@implementation NSMyObject
-(void) setI:(NSInteger)v ;
{
i=v;
}
- (void)dealloc {
[super dealloc];
}
@end
I call a function 'myFunction' with parameter temObj which is a NSMyObject instance.
myFunction(temObj);//temObj is NSMyObject
In the function, I can change the value of the instance variable of parameter obj
.
-(void)myFunction:(NSMyObject*) obj;
{
[obj setI:0];
}
... expecting this to change the content of temObj
.
But when I check the results of operation on obj
in function myFunction
the value of temObj.i
has not changed.
Welcome any comment
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够更改函数或方法内传递的对象的属性值。
我认为你的问题是上面代码中的
myFunction
没有定义为函数,而是定义为类的实例方法。它不会像这样独立工作:...相反,你必须这样称呼它:
我认为这就是你的问题所在。如果您尝试调用函数之类的方法,您应该会收到编译器警告。
如果正确调用该方法,您应该能够执行以下操作:
... 并查看刚刚设置打印到控制台的 obj.i 的值。
如果您确实正确调用了它,但 obj.i 的值仍然没有改变,最可能的解释是您查看的是 NSMyObject 的不同实例,而不是传递给该方法的实例。
You should be able to change the value of an attribute of a passed object inside a function or method.
I think your problem is that
myFunction
in the code above isn't defined as a function but rather the instance method of a class. It won't work standalone like this:... instead you have to call it like:
I think that is where your problem is. You should be getting a compiler warning if you try to call a method like a function.
If you call the method properly, you should be able to do this:
... and see the value of
obj.i
you just set printed to the console.If you do call it properly but the value of
obj.i
still doesn't change, the most likely explanation is that your looking at different instance of NSMyObject and not the one you passed to the method.我认为这样做没有任何问题。
但是,您的代码存在问题 - 不是技术上的问题,而是传统上的问题。
您不应该为自己的自定义对象添加 NS 前缀 - 这是 Apple 的 Foundation 框架前缀。
您应该在自己的类前面加上您的姓名缩写、应用程序名称或公司名称等前缀。Apple
在其框架中使用了许多前缀,如果您使用相同的前缀,您最终会遇到类名之间的冲突。
I don't see any problem doing this.
However there is a problem with your code - not technically, but conventionally.
You shouldn't be prefixing your own custom objects with NS - That's Apple's Foundation framework prefix.
You should prefix your own classes with an abbreviation of your name, or the app's name or your company name, etc.
Apple use many prefixes for their frameworks and if you use the same prefixes you'll eventually run into a clash between class names.
我在代码中没有看到任何语法问题,但是您可以遵循推荐的做法,例如,您可以将变量的属性设置为:
并且比在实现类中将其综合为
现在,当您要在方法中设置值时,代码应该完成这项工作:
另一件事是,尝试使用一些标准和推荐的编码约定,有时名称是关键字,即使它们在编译时不给出任何错误或警告,也会引起问题。
I dont see any syntax issue in your code, but you can follow the recommended practices like, you can make the property for your variable as:
And than in implementation class do synthesize it as
Now when you going to set the value in your method your code should do the job :
One more thing, try to use some standard and recommended coding conventions, sometime names are keywords and they cause problem even they dont give any error or warning at compile time.