UITableView 委托的问题
我有几个正在合成的字符串:
.h
@property(nonatomic,copy) NSString* bio;
.m
@synthesize bio;
//connectionDidFinish method
bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
当我的 tableview 第一次加载时,我收到错误 -[NSNull length]: unrecognized selector sent to instance 0x11d45e8
in cellForRowAtIndexPath:
case kBioSectionDescriptionRow:
if ([bio length]==0 ||bio == nil) {
cell.textLabel.text = @"bio";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.numberOfLines = 5;
cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:(14.0)];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = @"None";
}
else{
cell.textLabel.text = @"bio";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.numberOfLines = 5;
cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:(14.0)];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [NSString stringWithFormat: @"%@", bio];
}
break;
How can I make确定我的个人简介已分配且不为空?
I have a couple strings that I'm synthesizing:
.h
@property(nonatomic,copy) NSString* bio;
.m
@synthesize bio;
//connectionDidFinish method
bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
When my tableview first loads I get an error -[NSNull length]: unrecognized selector sent to instance 0x11d45e8
in cellForRowAtIndexPath:
case kBioSectionDescriptionRow:
if ([bio length]==0 ||bio == nil) {
cell.textLabel.text = @"bio";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.numberOfLines = 5;
cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:(14.0)];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = @"None";
}
else{
cell.textLabel.text = @"bio";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.detailTextLabel.numberOfLines = 5;
cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:(14.0)];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [NSString stringWithFormat: @"%@", bio];
}
break;
How can I make sure that my bio is allocated and not null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您
@synthesize
您的bio
属性,为什么要使用变量而不是属性?您不需要直接影响变量
bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
,而是需要影响属性本身:self.bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
。更准确的来说,写
self.bio = xx
意味着它调用了bio属性的setter方法。该设置器将为您管理内存,这意味着它将释放 bio 属性的先前值并复制新值。如果您改为编写
bio = xx
,从而直接影响实例变量而不是属性,则不会进行释放或复制,因此您影响到 bio 变量的对象不会保留或复制 并将在当前 RunLoop 结束时销毁。这就是您的代码崩溃的原因,因为您随后尝试访问
bio
变量,该变量不再指向任何内容(实际上它指向垃圾,在您的case to sthg 它错误地认为是[NSNull null]
对象),因为真正的对象已被销毁!实际上,
@synthesize bio
只是询问编译器生成代码property' setter 和 getter,并且由于您的属性是使用nonatomic,copy
属性定义的,生成的 setter 将如下所示:注意:不要忘记释放您的 bio 变量
dealloc
方法(或将self.bio
属性设置为nil
)以避免内存泄漏If you
@synthesize
yourbio
property, why do you use the variable instead of the property?Instead of affecting the variable
bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
directly, you need instead to affect the property itself:self.bio = [[[profile objectForKey:@"user"] valueForKey:@"profile"] valueForKey:@"bio"];
.To be more precise, writing
self.bio = xx
means that it calls the setter method of the bio property. This setter will manage the memory for you, meaning it will release the previous value of the bio property and copy the new value.If you write
bio = xx
instead, thus directly affecting the instance variable and not the property, no release or copy is done, so the objet you affect to the bio variable is not retained nor copied and will be destroyed at the end of the current RunLoop.This is why your code crash, because you then try to access the
bio
variable, which does not point to anything anymore (actually it points to garbage, in your case to sthg it erroneously believes to be the[NSNull null]
object) as the real object has been destroyed since!Actually,
@synthesize bio
just asks the compiler to generate the code for the property' setter and getter, and as your property is defined with thenonatomic,copy
attributes, the generated setter will look like this:Note: Don't forget to release the bio variable in your
dealloc
method (or to set theself.bio
property tonil
) to avoid leaking memory您在 -connectionDidFinish 中对 bio 的分配应该是对 self.bio 的分配。
Your assignment to bio in -connectionDidFinish should be an assignment to self.bio.