在Objective-C中声明静态变量的正确方法是什么? OK,当C、C++、C和Objul-C之间切换时,仍然会重新调整,所以有时我的头旋转。但是这次我更困惑了,因为我在ObjuleC中至少见过三种不同的方法来声明静态变量,如果你认为这只是C本身的一个超集,那么有第四的方法是正确的。那么,以下哪项是正确的 补充问题

在Objective-C中声明静态变量的正确方法是什么? OK,当C、C++、C和Objul-C之间切换时,仍然会重新调整,所以有时我的头旋转。但是这次我更困惑了,因为我在ObjuleC中至少见过三种不同的方法来声明静态变量,如果你认为这只是C本身的一个超集,那么有第四的方法是正确的。那么,以下哪项是正确的 补充问题,objective-c,static-variables,Objective C,Static Variables,如果我们想要共享一个独立变量(即不是一个静态类变量,而是一个在头文件中定义的变量),这与在“C”(头文件中带有“extern”的ala)中的方式相同吗 方案A Foo.h @interface Foo : NSObject{ static int Laa; } @end @interface Foo : NSObject{ } @end @interface Foo : NSObject{ } @end static int Laa; @interface Foo : N

如果我们想要共享一个独立变量(即不是一个静态类变量,而是一个在头文件中定义的变量),这与在“C”(头文件中带有“extern”的ala)中的方式相同吗


方案A Foo.h

@interface Foo : NSObject{
    static int Laa;
}

@end
@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
static int Laa;

@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
Foo.m

@implementation Foo
    ...
@end
static int Laa; // <-- Outside of the implementation

@implementation Foo
    ...
@end
int Laa; // <-- Note no word 'static' here like in 'Option B'

@implementation Foo
    ...
@end
@implementation Foo
    ...
@end
@implementation Foo

    static int Laa;

    ...

@end

方案B Foo.h

@interface Foo : NSObject{
    static int Laa;
}

@end
@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
static int Laa;

@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
Foo.m

@implementation Foo
    ...
@end
static int Laa; // <-- Outside of the implementation

@implementation Foo
    ...
@end
int Laa; // <-- Note no word 'static' here like in 'Option B'

@implementation Foo
    ...
@end
@implementation Foo
    ...
@end
@implementation Foo

    static int Laa;

    ...

@end
Foo.m

@implementation Foo
    ...
@end
static int Laa; // <-- Outside of the implementation

@implementation Foo
    ...
@end
int Laa; // <-- Note no word 'static' here like in 'Option B'

@implementation Foo
    ...
@end
@implementation Foo
    ...
@end
@implementation Foo

    static int Laa;

    ...

@end
Foo.m

@implementation Foo
    ...
@end
static int Laa; // <-- Outside of the implementation

@implementation Foo
    ...
@end
int Laa; // <-- Note no word 'static' here like in 'Option B'

@implementation Foo
    ...
@end
@implementation Foo
    ...
@end
@implementation Foo

    static int Laa;

    ...

@end

选项E Foo.h

@interface Foo : NSObject{
    static int Laa;
}

@end
@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
static int Laa;

@interface Foo : NSObject{
}

@end
@interface Foo : NSObject{
}

@end
Foo.m

@implementation Foo
    ...
@end
static int Laa; // <-- Outside of the implementation

@implementation Foo
    ...
@end
int Laa; // <-- Note no word 'static' here like in 'Option B'

@implementation Foo
    ...
@end
@implementation Foo
    ...
@end
@implementation Foo

    static int Laa;

    ...

@end

奖金问题。。。

你必须使用“代码>外部”/CODE >还是仅当你在使用C/C++文件时,而不是.m/mm文件?

< p>因为你提供了多个选择,所以我必须与选项A一起使用。这是一个静态变量,只能在类内可见。为了从外部访问它,您需要编写一个类似于的自定义getter和setter方法。

这取决于您所说的“静态”是什么意思。Objective-C得到了最大值,所以它与类无关。如果您试图在C++中创建一个类似的对象,那么Objto-C等价物只是代码< >代码>文件中的全局。如果在声明全局变量时使用static关键字,则可以确保变量不能是
extern
'd并在其他文件中使用,这可能是您所追求的。

选项A是错误的。目标-c类没有静态变量。
选项BE是实现静态变量的正确方法。
选项C创建一个全局变量,可以使用extern关键字从实现文件外部访问该变量。
选项D再次创建一个全局静态变量,只需导入.h文件即可从任何位置访问该变量。

关于您的附加问题:extern关键字的含义与C/C++中的相同,如果我错了,请纠正我,但属性不是特定于实例的东西,不是类级静态,这意味着您必须拥有一个公开内部静态的类实例?这将类似地工作:您只需自己手动编写set/get方法,而不是使用@synthesis.you是正确的。不过,你可以自己制定这些方法。请看这里,我编辑了我的答案-我不是静态变量的常客。哈哈,hivemind:在30秒内用相同的链接回答。所以暂时把类从等式中去掉,你是说如果在.m文件中我只声明
int foo我可以像在C中一样在.h文件中对其进行外部处理。但是,如果我将其声明为
static int foo,同样在.m文件中,它在其他方面完全相同,但不能被外部化?Pt 2:在上面的@implementation,ala选项E中声明变量(带或不带静态字)怎么样?我不认为这是可能的,但这是另一个问题的答案。是的,默认情况下globals可以是
extern
'd,但设置全局静态将禁用它。至于第二点,在
@implementation
块中声明全局变量对它没有任何影响。它仍然只是一个普通的全局方法。所以选项D相当于使用extern,对吗?实际上我来这里是想知道B和E中的哪一个是规范方法,但无论如何+1,因为其他答案没有抓住问题的重点。我找不到明确的参考,但惯例似乎更喜欢选项E(即使是XCode示例项目似乎也是如此)。@Manav是的,苹果示例使用了E选项,据我所知,E&B没有任何区别。如果你有不同的想法,那将是一件我很想知道的事情。