gpt4 book ai didi

objective-c - 即使在 alloc/init 之后,我的 NSMutableArray 也会在函数范围之外丢失它的对象

转载 作者:行者123 更新时间:2023-11-28 18:14:16 25 4
gpt4 key购买 nike

我完全被这个难住了。我有一个 NSMutableArray,它在我的 header 中声明并设置为属性、合成等。然后我调用一个函数来分配和初始化数组,并向其中添加自定义对象。添加对象后,我为每个循环执行一个循环,以确保它们实际上包含在数组中,并且确实如此。但是,一旦程序超出此函数范围,数组就会突然变空。

头文件:

@interface ScheduleViewController : UITableViewController  {

NSString *login_id;
NSMutableArray *events;
}

- (id)initWithID:(NSString*)l_id;
- (void)grabURLInBackground; // ASIHTTP example method

@property (nonatomic, retain) NSString *login_id;
@property (nonatomic, retain) NSMutableArray *events;

@end

实现:

@synthesize events;

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *response = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *eventDics = [parser objectWithString:response error:nil]; // an array of dictionaries of events
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];

// Allocate empty event object and initialize the mutable array
Event* event = [[Event alloc] init];
self.events = [[NSMutableArray alloc] initWithCapacity:[eventDics count]];

// loop through the array of dictionaries
for (int i = 0; i < [eventDics count]; i++)
{
NSDictionary *dict = [eventDics objectAtIndex:i];
for(NSString *key in dict) {
// for the sake of readability i wont include the code
// but the event is populated here
}
[self.events addObject:event];
[event release];
}
NSLog(@"Array Count: %i", [self.events count]);
for (Event *e in events) {
NSLog(@"eventid: %i, type: %@, price: %f, name: %@", e.event_id, e.type, e.price, e.name);
}
[parser release];
[dateForm release];
}

所以上面的代码工作正常并打印出存储在事件可变数组中的事件对象的变量。

我现在想做的是在另一个函数中使用事件数组,当我尝试这样做时,计数为 0,而且当我查看它时,数组中也没有存储任何对象。

在 viewDidUnload 中我设置了 self.events = nil;在 dealloc 中我做 [self.events release]

最佳答案

您在 for 循环之外为 Event *event 对象执行分配/初始化。这意味着每次将对象添加到数组时都在添加相同的对象。你应该移动这一行:

Event* event = [[Event alloc] init];

到你的里面

for (int i=0 ...  loop.

不确定这是否可以解释您所看到的症状,但它可以,因为以下声明:

[event release] 

还在循环中每次释放一个分配的对象一次 - 所以您要多次释放该对象。如果将 Event alloc 移动到循环内部,那么释放就可以了。 (将对象添加到数组中将保留它,因此可以释放它,但您需要在循环中每次分配一个新的事件)。

基本上您的代码应该如下所示:(请注意,我还向您的数组分配添加了一个自动释放)。

// Allocate empty event object and initialize the mutable array
self.events = [[[NSMutableArray alloc] initWithCapacity:[eventDics count]] autorelease]; // assigning to the retain property will retain it, so autorelease it or it will be retained twice. Could also have used the arrayWithCapacity convenience method here instead and then wouldn't need to autorelease.

// loop through the array of dictionaries
for (int i = 0; i < [eventDics count]; i++)
{
Event* event = [[Event alloc] init]; // Allocate a new Event each time through the loop so you are adding a unique object to the array each time.
NSDictionary *dict = [eventDics objectAtIndex:i];
for(NSString *key in dict) {
// for the sake of readability i wont include the code
// but the event is populated here
}
[self.events addObject:event];
[event release];
}

关于objective-c - 即使在 alloc/init 之后,我的 NSMutableArray 也会在函数范围之外丢失它的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com