I'll try and keep this simple.
我会尽量保持这个简单。
.h
NSMutableArray *allEventsData;
@property (nonatomic, retain) NSMutableArray *allEventsData;
.m
-(void) test {
DAL *db = [[DAL alloc] init]
self.allEventsData = [[NSMutableArray alloc] initWithArray:[db readUniversityEvents]];
//to test
for(int i = 0; i < [allEventsData count]; i++) {
UniversityEvents *obj = (UniversityEvents*)[self.allEventsData objectAtIndex:i];
NSLog(@"%@", obj.eventId);
}
}
Now that works just fine, however the second this void method is left, I can never access it again, the count property shows that there are still the same amount of records, however if I try the for loop, it will just break out and I'll get Program received signal: "EXC_BAD_ACCESS"
现在它工作得很好,但是第二个这个void方法仍然存在,我再也无法访问它了,count属性显示仍然有相同数量的记录,但是如果我尝试for循环,它将会爆发并且我会收到程序收到的信号:“EXC_BAD_ACCESS”
I'm being a little vague, but I'm hoping for some suggestions as to why this happens.
我有点模糊,但我希望能找到一些关于为什么会这样的建议。
Thanks
PS Had to type than manually, so please ignore possible spelling errors.
PS必须手动打字,所以请忽略可能的拼写错误。
Edit: Just to clarify, I'm storing an array of objects within a new array, could it be that allEventsData is simply pointing to those objects and not retaining a copy? That would explain the bad access exception
编辑:只是为了澄清,我在一个新数组中存储一个对象数组,可能是allEventsData只是指向那些对象并且没有保留副本吗?这可以解释糟糕的访问异常
EDIT 2:
FYI due to my suspicions above, I also tried the following, however I get SIGABRT exception, currently looking into a solution
由于我上面的怀疑,我也试过以下,但是我得到SIGABRT例外,目前正在寻找一个解决方案
self.allEventsData = [[NSMutableArray alloc] initWithArray:[db readUniversityEvent] copyItems:YES];
Log:
-[UniversityEvents copyWithZone:]: unrecognized selector sent to instance 0xdd17f50
2012-01-16 12:13:17.789 sqliteDemo[1211:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UniversityEvents copyWithZone:]: unrecognized selector sent to instance 0xdd17f50'
*** Call stack at first throw:
(
0 CoreFoundation 0x00f905a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010e4313 objc_exception_throw + 44
2 CoreFoundation 0x00f920bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f01966 ___forwarding___ + 966
4 CoreFoundation 0x00f01522 _CF_forwarding_prep_0 + 50
5 CoreFoundation 0x00ef0dca -[NSObject(NSObject) copy] + 42
6 CoreFoundation 0x00f8bff2 -[NSArray initWithArray:range:copyItems:] + 290
7 CoreFoundation 0x00ef3e33 -[NSArray initWithArray:copyItems:] + 99
8 sqliteDemo 0x000030f4 -[sqliteDemoViewController displayData] + 242
9 sqliteDemo 0x00002e30 -[sqliteDemoViewController updateData] + 427
10 UIKit 0x003e64fd -[UIApplication sendAction:to:from:forEvent:] + 119
11 UIKit 0x005f8cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
12 UIKit 0x003e64fd -[UIApplication sendAction:to:from:forEvent:] + 119
13 UIKit 0x00476799 -[UIControl sendAction:to:forEvent:] + 67
UniversityEvents (this is the object which is being stored within the class) .h
UniversityEvents(这是存储在类中的对象).h
@interface UniversityEvents : NSObject {
}
@property (assign) NSString * eventID;
@property (retain) NSString * title;
@property (retain) NSString * url;
@property (retain) NSString * category;
@property (retain) NSString * date;
@property (retain) NSString * startTime;
@property (retain) NSString * endTime;
@property (retain) NSString * department;
@property (retain) NSString * location;
@property (retain) NSString * description;
//@property (assign) NSInteger * attending;
-(id) initWithData :(NSString *) _eventID :(NSString *) _title :(NSString *)_url :(NSString *)_category :(NSString *)_date
:(NSString *)_startTime :(NSString *) _endTime :(NSString *)_department :(NSString *)_location
:(NSString *) _description;
@end
.m @synthesize eventID , title,url,category,date,startTime,endTime,department,location,description;
.m @synthesize eventID,title,url,category,date,startTime,endTime,department,location,description;
-(id) initWithData :(NSString *) _eventID :(NSString *) _title :(NSString *)_url :(NSString *)_category :(NSString *)_date
:(NSString *)_startTime :(NSString *) _endTime :(NSString *)_department :(NSString *)_location
:(NSString *) _description
{
self = [super init];
if(self)
{
self.eventID = _eventID;
self.title = _title;
self.url = _url;
self.category = _category;
self.date = _date;
self.startTime = _startTime;
self.endTime = _endTime;
self.department = _department;
self.location = _location;
self.description = _description;
//self.attending = _attending;
}
return self;
}
@end
2 个解决方案
#1
1
Change
@property (assign) NSString * eventID;
to
@property (retain) NSString * eventID;
#2
0
There is a difference between [allEventsData count]
(accessing the ivar) and self.allEventsData
(access the property). Also, did you log each UniversityEvents
element ? If one of them is nil you can't access its eventId
property. We might need more code, maybe from the method readUniversityEvents
[allEventsData count](访问ivar)和self.allEventsData(访问属性)之间存在差异。另外,您是否记录了每个UniversityEvents元素?如果其中一个为零,则无法访问其eventId属性。我们可能需要更多代码,可能来自方法readUniversityEvents
#1
1
Change
@property (assign) NSString * eventID;
to
@property (retain) NSString * eventID;
#2
0
There is a difference between [allEventsData count]
(accessing the ivar) and self.allEventsData
(access the property). Also, did you log each UniversityEvents
element ? If one of them is nil you can't access its eventId
property. We might need more code, maybe from the method readUniversityEvents
[allEventsData count](访问ivar)和self.allEventsData(访问属性)之间存在差异。另外,您是否记录了每个UniversityEvents元素?如果其中一个为零,则无法访问其eventId属性。我们可能需要更多代码,可能来自方法readUniversityEvents