UITableView 委托的问题

发布于 2024-11-24 17:54:15 字数 1626 浏览 2 评论 0原文

我有几个正在合成的字符串:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-12-01 17:54:15

如果您@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 将如下所示:

-(void)setBio:(NSString*)value {
  if (value == bio) return; // if already the exact same object (same pointer), return

  [self willChangeValueForKey:@"bio"]; // For KVO
  [bio release]; // release previous value
  bio = [value copy]; // copy new value
  [self didChangeValueForKey:@"bio"]; // For KVO
}

注意:不要忘记释放您的 bio 变量dealloc 方法(或将 self.bio 属性设置为 nil)以避免内存泄漏

If you @synthesize your bio 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 the nonatomic,copy attributes, the generated setter will look like this:

-(void)setBio:(NSString*)value {
  if (value == bio) return; // if already the exact same object (same pointer), return

  [self willChangeValueForKey:@"bio"]; // For KVO
  [bio release]; // release previous value
  bio = [value copy]; // copy new value
  [self didChangeValueForKey:@"bio"]; // For KVO
}

Note: Don't forget to release the bio variable in your dealloc method (or to set the self.bio property to nil) to avoid leaking memory

り繁华旳梦境 2024-12-01 17:54:15

您在 -connectionDidFinish 中对 bio 的分配应该是对 self.bio 的分配。

Your assignment to bio in -connectionDidFinish should be an assignment to self.bio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文