在NSMutableArray存储对象中丢失数据

时间:2012-01-16 11:01:09

标签: iphone objective-c xcode

我会尽量保持这一点。

.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);

        }
}

现在它工作正常,但是第二个这个void方法留下了,我再也无法访问它了,count属性显示仍然有相同数量的记录,但是如果我尝试for循环,它只会爆发,我会收到程序收到的信号:“EXC_BAD_ACCESS”

我有点模糊,但我希望能提出一些有关为何会发生这种情况的建议。

谢谢

PS必须手动打字,所以请忽略可能的拼写错误。

编辑:为了澄清一下,我在一个新数组中存储了一个对象数组,是不是allEventsData只是指向那些对象并且没有保留副本?这可以解释糟糕的访问异常

编辑2:

由于我上面的怀疑,我也试过以下,但是我得到了SIGABRT例外,目前正在研究解决方案

self.allEventsData = [[NSMutableArray alloc] initWithArray:[db readUniversityEvent] copyItems:YES];

日志:

-[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(这是存储在类中的对象) .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;

-(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 个答案:

答案 0 :(得分:1)

更改

@property (assign) NSString * eventID;

@property (retain) NSString * eventID;

答案 1 :(得分:0)

[allEventsData count](访问ivar)和self.allEventsData(访问该属性)之间存在差异。另外,您是否记录了每个UniversityEvents元素?如果其中一个为零,则无法访问其eventId属性。我们可能需要更多代码,可能来自方法readUniversityEvents

相关问题