返回指针后 NSArray 对象超出范围
我尝试在函数中使用以下代码来返回字典对象数组。不幸的是,在返回到堆栈中的下一个函数之后,可变数组中的所有行都变得“超出范围”。根据我的理解,数组应该自动保留行(字典)对象,因此即使在返回后,行指针超出范围,行对象的保留计数仍应为 1。我在这里做错了什么?如何构建这个数组,使其包含的对象不会被释放?
for (int i = 1; i < nRows; i++)
{
NSMutableDictionary* row = [[[NSMutableDictionary alloc] initWithCapacity:nColumns] ];
for(int j = 0; j < nColumns; j++)
{
NSString* key = [[NSString stringWithUTF8String:azResult[j]] ];
NSString* value = [[NSString stringWithUTF8String:azResult[(i*nColumns)+j]] ];
[row setValue:value forKey:key];
}
[dataTable addObject:row];
}
return dataTable;
I am attempting to use the below code in a function to return an array of dictionary objects. Unfortunately, after the return to the next function in the stack all of the rows in the mutable array have become 'out of scope'. From my understanding, the array should retain the row (dictionary) object automatically so even after the return, where the row pointer goes out of scope, the row objects should still have a retain count of 1. What am I doing wrong here? How do I build this array in such a way that the objects it contains don't get released?
for (int i = 1; i < nRows; i++)
{
NSMutableDictionary* row = [[[NSMutableDictionary alloc] initWithCapacity:nColumns] ];
for(int j = 0; j < nColumns; j++)
{
NSString* key = [[NSString stringWithUTF8String:azResult[j]] ];
NSString* value = [[NSString stringWithUTF8String:azResult[(i*nColumns)+j]] ];
[row setValue:value forKey:key];
}
[dataTable addObject:row];
}
return dataTable;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一行:
应该使用自动释放:
This line:
should use the autorelease:
据我了解:
您应该查看 callMethod 中的局部变量,而不是 dataTable ,它是我称为 getArrayOfDictionaries 的方法的本地变量
From what i understand:
You should look into the local variable in callingMethod instead of dataTable which is local to the method I called getArrayOfDictionaries