Objective-C:添加对象后数组中没有对象。超出范围!

发布于 2024-12-16 14:30:33 字数 958 浏览 0 评论 0原文

我的对象中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昨迟人 2024-12-23 14:30:33

只需在使用该数组之前在某处分配工作数据库(可变数组)即可。

一旦你分配了它,它就会正常工作。

just allocate your workingDatabases (Mutable array) somewhere before using that array.

Once you allocate it,It will work fine.

拔了角的鹿 2024-12-23 14:30:33

我认为这是因为 [LRResty client] get: 是异步的。当连接完成时,即在调用返回之后,将调用该块。

//Called first
[[LRResty client] get:connectURL

//Called second
return workingDatabases;

//Called later when the connection is finished
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];
    }

编辑

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.

//Called first
[[LRResty client] get:connectURL

//Called second
return workingDatabases;

//Called later when the connection is finished
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];
    }

Edit

Ajeet has a valid point too, ensure your array is initialized.

掩于岁月 2024-12-23 14:30:33

我使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文