Ios Objective-c非圆弧对象
我正在管理一个旧的Objective-c项目,它仍然使用Ios Objective-c非圆弧对象,ios,objective-c,xcode,nsmutablearray,Ios,Objective C,Xcode,Nsmutablearray,我正在管理一个旧的Objective-c项目,它仍然使用非Arc,我在Fabric Crashlytics中注意到应用程序中的EXC\u BAD\u ACCESS KERN\u INVALID\u ADDRESS崩溃 这是坠机的痕迹,我有以下代码行: [view.label setText:[historyArray getStringForVideoArray]]; 调用此函数: -(NSString*)getStringForVideoArray { NSString *str =
非Arc
,我在Fabric Crashlytics
中注意到应用程序中的EXC\u BAD\u ACCESS KERN\u INVALID\u ADDRESS
崩溃
这是坠机的痕迹,我有以下代码行:
[view.label setText:[historyArray getStringForVideoArray]];
调用此函数:
-(NSString*)getStringForVideoArray {
NSString *str = nil;
int seconds = 0;
for (NSObject *video in self) {
if (!video) {
continue;
}
if ([video isKindOfClass:[VideoItem class]]) {
VideoItem *tmp = (VideoItem*)video;
seconds += tmp.seconds;
}
}
if (seconds != 0) {
int minutes = seconds / 60;
if (minutes == 0 || minutes == 1) {
minutes = 1;
str = [NSString stringWithFormat:@"%d Min.",minutes];
} else {
str = [NSString stringWithFormat:@"%d Mins.",minutes];
}
} else {
str = [NSString stringWithFormat:@""];
}
return str;
}
从Fabric Crashlytics
中我注意到,崩溃发生在以下几行:
if ([video isKindOfClass:[VideoItem class]]) {
我所能想到的唯一能造成这次崩溃的是用这种方法创建的historyArray
:
historyArray = [[NSMutableArray alloc] initWithArray:historyRep.historyArray];
这是类中数组的定义:
@interface HistoryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *historyArray;
}
2) 插入其他类中的项:
[self.historyArray插入对象:视频索引:0];
由以下内容创建:
if (self.videoItem)
[self.videoItem release];
self.videoItem = [[VideoItem alloc] initWithVideoItem:item];
3) 第一次使用以下命令初始化阵列:
NSArray *myRepository = [[NSUserDefaults standardUserDefaults] arrayForKey:@"kHistory"];
if(myRepository) {
for(NSData *data in myRepository) {
VideoItem *video = (VideoItem*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
[self.historyArray addObject:video];
}
}
有什么建议会导致这个问题吗?因为这是一个可变数组,所以可以在迭代过程中清理它,从而导致恰好在报告的行处崩溃(因为前面的条件为true,所以即使是悬挂的指针也可以通过,而且它是没有意义的) 这里有可能的解决办法
-(NSString*)getStringForVideoArray{
NSString*str=nil;
整数秒=0;
NSArray*tmp=[NSArray arrayWithArray:self];//浅拷贝-快速
用于(tmp中的NSObject*视频){
if([video iskindof类:[VideoItem类]]){
VideoItem*tmp=(VideoItem*)视频;
秒+=tmp.seconds;
}
}
…//下面所有内容都没有更改
}
为什么用于(NSObject*视频自身)
?而不是(NSObject*video in historyArray)
?@特洛伊木马,因为OP似乎已在NSArray
中将其作为一个类别添加,这通常不推荐,但是合法的。很可能是您过度发布了阵列中的一个VideoItem
对象。您需要审核所有这些项目的保留和发布。@RobNapier;我错过了他在historyArray
@RobNapier上调用的方法我只是用我向数组添加项目的方式更新我的问题我只是用我向数组添加项目的方式更新我的问题
NSArray *myRepository = [[NSUserDefaults standardUserDefaults] arrayForKey:@"kHistory"];
if(myRepository) {
for(NSData *data in myRepository) {
VideoItem *video = (VideoItem*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
[self.historyArray addObject:video];
}
}