简体   繁体   English

NSMutableArray子类的Count方法使应用程序崩溃

[英]Count method of subclass of NSMutableArray crashes app

This seems to be a common problem, but I can't figure out anything from the answers I've seen so far. 这似乎是一个普遍的问题,但到目前为止我仍然无法从答案中找出任何答案。 I have an iPhone app that uses a subclass of NSMutableArray to store objects, plus some additional properties. 我有一个iPhone应用程序,该应用程序使用NSMutableArray的子类来存储对象以及一些其他属性。 The subclass is skhCustomArray . 子类是skhCustomArray The subclass initializes fine, with no objects in the skhCustomArray , and I assign it to the the property of my view controller, which is a pointer to an skhCustomArray . 子类初始化良好,在skhCustomArray没有对象,然后将其分配给视图控制器的属性,该属性是skhCustomArray的指针。

    prescriptionListVC* newPrescList = [[prescriptionListVC alloc] initWithNibName:@"PrescriptionList" bundle:nil];
    newPrescList.curPersonPrescriptions = [personDetails objectAtIndex:0];

That works fine. 很好 Yet when I push my view managed by my view controller onto the navigation controller stack, the count method in the numberOfRowsInSection method crashes the app, see below. 但是,当我将由视图控制器管理的视图推入导航控制器堆栈时, numberOfRowsInSection方法中的count方法会使应用程序崩溃,请参见下文。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [curPersonPrescriptions count];

} }

What could be causing this? 是什么原因造成的? How can a valid custom array, with no objects, not return a valid count? 没有对象的有效自定义数组如何不返回有效计数? Where am I going wrong? 我要去哪里错了? Thanks. 谢谢。

Subclass of NSArray? NSArray的子​​类? You're aware that NSArray is a class cluster , and is therefore somewhat difficult to subclass, right? 您知道NSArray是一个类集群 ,因此很难进行子类化,对吧? In fact, it's so fraught with danger that the NSArray documentation has a whole section dedicated to what you need to do in order to subclass it . 实际上,这充满了危险,NSArray文档中有一整节专门介绍了如何将其子类化

I'll bet that that's the source of your woes. 我敢打赌,这就是你麻烦的根源。

When you subclass NSMutableArray, you need to implement some mandatory methods like count, addObject:, insertObjectAtIndex etc. This is what we call as class cluster. 当您将NSMutableArray子类化时,您需要实现一些强制性方法,例如count,addObject:,insertObjectAtIndex等。这就是我们所说的类集群。

If you want to add some more feature/behavior to already implemented object then you can write a "category" instead of "subclassing" it. 如果要向已实现的对象添加更多功能/特性,则可以编写“类别”而不是“子类化”。

If you want to subclass it, then you have to implement all those methods which your are going to use so better write a category of NSMutableArray and extend the feature what you want and use the NSMutableArray object only. 如果要对其进行子类化,则必须实现将要使用的所有那些方法,以便更好地编写NSMutableArray的类别并扩展所需的功能并仅使用NSMutableArray对象。 This will solve your problem and also this is the easy and almost right way to add new behavior to already existing class. 这将解决您的问题,这也是向现有类中添加新行为的简单且几乎正确的方法。

You almost certainly don't need to subclass NSMutableArray in this situation. 在这种情况下,您几乎可以肯定不需要子类NSMutableArray。 Instead, make a new class which has an array as a property , along with the extra properties you desire. 相反,创建一个新类,该类具有一个数组作为property ,以及所需的其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM