小编Bkn*_*nee的帖子

NSString实例变量的奇怪行为

以下是使用引用计数(ARC)的iOS程序中类定义的一部分:

@interface CapViewController : UIViewController 
{
    NSString *bottomBn;
    NSString *topBn;
}

@property (nonatomic, strong) NSString *bottomBn;
@property (nonatomic, strong) NSString *topBn;

@end
Run Code Online (Sandbox Code Playgroud)

在实现中我合成它们:

@implementation CapViewController

@synthesize bottomBn;
@synthesize topBn;
Run Code Online (Sandbox Code Playgroud)

问题是当我尝试分配值时.如果我在类方法中逐步执行以下行(第一次使用每个实例变量):

bottomBn = [NSString stringWithString:@"bottomBn"];        
topBn = [NSString stringWithString:@"topBn"];
Run Code Online (Sandbox Code Playgroud)

第一行执行后,topBn的值变为@"bottomBn",bottomBn变为nil第二行没有影响.

如果我更改了顺序,则在类中定义实例变量,即:

NSString *topBn;
NSString *bottomBn;
Run Code Online (Sandbox Code Playgroud)

然后第一个赋值没有效果,第二个赋值导致"topBn"被赋值给bottomBn.

使用局部变量,它按预期工作:

NSString *localbottomBn = [NSString stringWithString:@"defaultbottombutton"];        
NSString *localtopBn = [NSString stringWithString:@"defaulttopbutton"];
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很奇怪.我很感激任何帮助.

objective-c nsstring ios

2
推荐指数
1
解决办法
274
查看次数

标签 统计

ios ×1

nsstring ×1

objective-c ×1