Закрытый блок, объявленный внутри блока @implementation
, кажется мне опасным по сравнению с другими концепциями ООП, например, Джава. Это похоже на переменную-член, но немного статично.
Начинающий программист может легко обмануть его. Я пишу тестовую программу и удивляюсь поведению.
@interface SomeClass : NSObject
{
NSString *forExample;
}
- (void) set:(NSString *)one another:(NSString *)another;
- (void)print;
@end
Реализация:
#import "SomeClass.h"
@implementation SomeClass
NSString *anotherExample;
- (void) set:(NSString *)one another:(NSString *)another
{
forExample = one;
anotherExample = another;
}
- (void)print{
NSLog(@"One = %@, another = %@", forExample, anotherExample);
}
@end
Тест:
- (void)testClass {
SomeClass * s1 = [SomeClass new];
[s1 set:@"one one" another:@"one another"];
SomeClass *s2 = [SomeClass new];
[s2 set:@"two one" another:@"two another"];
[s1 print];
[s2 print];
}
И вывод,
One = one one, another = two another
One = two one, another = two another