Objective-C:添加对象后数组中没有对象。超出范围!
我的对象中有一个 NSMutableArray 。 在对象方法中,我做了这样的事情:
/* ... */
[[LRResty client] get:connectURL withBlock:^(LRRestyResponse *r) {
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonResponse = [jsonParser objectWithString:[r asString]];
NSDictionary *permittedBases= [jsonResponse objectForKey:@"permittedBases"];
Database *database = [[Database alloc] init];
for (id key in permittedBases) {
/* ... */
[workingDatabases addObject:database];
}
}];
return workingDatabases;
在返回行,我的数组中没有对象(不再)。我知道“数据库”对象超出了范围。但我将它们保存在数组中。
我在监督什么吗?
如果有任何帮助,这里是头文件:
@class Database;
@interface CommunicationHelper : NSObject {
NSMutableArray *workingDatabases;
}
// The function where the problem appears:
- (NSMutableArray *)getDatabasesForWebsite:(Website *)websiteIn;
@property(nonatomic,copy) NSMutableArray *workingDatabases;
@end
I have a NSMutableArray in an object.
In an object-method, I do something like this:
/* ... */
[[LRResty client] get:connectURL withBlock:^(LRRestyResponse *r) {
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonResponse = [jsonParser objectWithString:[r asString]];
NSDictionary *permittedBases= [jsonResponse objectForKey:@"permittedBases"];
Database *database = [[Database alloc] init];
for (id key in permittedBases) {
/* ... */
[workingDatabases addObject:database];
}
}];
return workingDatabases;
At the return line, there are no objects in my array (anymore). I am aware of the fact, that the 'database'-objects are going out of scope. But I am saving them in the array.
Am I overseeing something?
If it is of any help, here is the header file:
@class Database;
@interface CommunicationHelper : NSObject {
NSMutableArray *workingDatabases;
}
// The function where the problem appears:
- (NSMutableArray *)getDatabasesForWebsite:(Website *)websiteIn;
@property(nonatomic,copy) NSMutableArray *workingDatabases;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在使用该数组之前在某处分配工作数据库(可变数组)即可。
一旦你分配了它,它就会正常工作。
just allocate your workingDatabases (Mutable array) somewhere before using that array.
Once you allocate it,It will work fine.
我认为这是因为
[LRResty client] get:
是异步的。当连接完成时,即在调用返回之后,将调用该块。编辑
Ajeet 也有一个有效点,确保您的数组已初始化。
I assume it's because
[LRResty client] get:
is asynchronous. The block is called when the connection is finished, i.e. after the call to return.Edit
Ajeet has a valid point too, ensure your array is initialized.
我使用 LRResty 框架来访问 RESTful Web 服务。无论如何,这都是一件奇怪的事情,所以我改用了一种功能更丰富的框架,称为“ASIHTTP”。我会推荐给任何想要在 iOS 上使用 RESTful 服务(以及更多)的人
I used the LRResty framework for accessing a RESTful webservice. It was an odd thing anyways, so I switched to a way more rich-featured framework, called "ASIHTTP". I would recommend that to anyone who wants to use RESTful services (and more) on iOS