问题描述
好吧,这几天我已经读了很多关于这个话题的文章,而且我也很困惑,因为每次搜索时答案都是不同的.
Ok, I've read a lot around these days about this topic and I alwyas get confused because the answers is different every search I make.
我需要知道在iOS中声明实例变量的最佳方法.到目前为止,我知道我只应该在.m文件中声明它们,并保持.h干净.但是我做不到:编译器给了我编译错误.
I need to know the best way to declare instance variables in iOS. So far I know I should only declare them inside .m file and leave .h clean. But I can't do it: the compiler gives me compilation erros.
这里仅是.m中的一些代码.
Here is some code from .m only.
@interface UIDesign ()
// .m file
{
NSString *test2 = @"test2";
}
@property (nonatomic, assign) int privateInt;
@end
@implementation UIDesign
{
NSString *test1 = @"test1";
}
两个字符串的声明都不正确,我也不知道为什么.编译器说:期望';'在声明列表的末尾.
Both strings are declared incorrectly and I don't know why. The compiler says: expected ';' at end of declaration list.
所以问题是:如何声明实例变量?我只需要在班级里使用它们即可.
推荐答案
您无法初始化实例变量.它们都被初始化为nil或零.因此,编译器在编写等号时会期望使用分号.
You cannot initialize instance variables. They are all initialized to nil or zeroes. So compiler expect a semicolon when you are writing an equal sign.
您可以使用init方法初始化它们.
You can initialize them in init method.
这篇关于在iOS中声明实例变量-Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!