gpt4 book ai didi

{ } 内的 objective-c 变量声明 vs @interface 和 @end 外

转载 作者:行者123 更新时间:2023-11-28 18:25:27 24 4
gpt4 key购买 nike

谁能告诉我在 { } 内声明变量与在头文件中 @interface 和 @end 之外声明变量有什么区别。这两种方式都会导致编译成功。

最佳答案

如果您希望变量成为类的一部分(又名实例变量),则在括号内声明它,否则它被声明为与类无关的全局变量。

例如

// XYZ.h

@interface XYZ
{
int myinstancevariable;
}
@end

int myglobalvariable;

...

XYZ* a = [[XYZ alloc] init];
NSLog(@"%d", [a myinstancevariable]);
NSLog(@"%d", myglobalvariable );

编辑:忘了{}

关于{ } 内的 objective-c 变量声明 vs @interface 和 @end 外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10975803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com