7
请看看这个标题:Objective-C中的不同静态变量声明如何?
// Test.h
@interface Test : NSObject @end
extern id A; // (0)
//extern static id B; // (1) Uncomment to get a compiling error
extern id C; // (2)
//extern static id D; // (3) Uncomment to get a compiling error
而进入该实现:
// Test.m
#import "Test.h"
id A = @"A"; // (4)
static id B = @"B"; // (5)
@implementation Test
id C = @"C"; // (6)
static id D = @"D"; // (7)
@end
// Still Test.m
@interface Test2 : NSObject @end
@implementation Test2 : NSObject
+ (void)initialize {
NSLog(@"%@ %@", A, B); // (8)
NSLog(@"%@ %@", C, D); // (9)
}
@end
我有以下问题:
- 是否有声明(4)之间的任何根本区别(5)或(6)和(7)?
- “外部”声明(4)和附在实现范围(6)之间是否有区别?
- 为什么在实现范围内声明的(6)和(7)可以在另一个实现范围(9)中访问?
- 为什么(2)在头部声明可以访问(6)在实现范围内声明?
- 为什么(1)和(3)产生错误
Cannot combine with previous 'extern' declaration specifier
,但(0)和(2)编译时没有错误?
哇,你摇滚。谢谢! – Stream 2012-07-08 11:12:07