NSMutable 数组:“超出范围”可变复制后的状态

发布于 2024-12-08 17:22:58 字数 2054 浏览 0 评论 0原文

我有一个 SOAP 服务,并在 SudzC.com 上生成了类和函数。

所以我使用他们生成的soap函数,它返回一个NSMutableArray,其中包含由我的自定义类继承的对象(也是由他们生成的)。

到目前为止一切都很好。我的值正在进入数组,并且我可以在一个条件下看到任何对象的任何属性:仅在处理服务的函数内部。

为了说清楚,这里是代码:

- (void)viewDidLoad
{
    SDZGeneratedWebService* service = [SDZGeneratedWebService service];
    service.logging = YES;

    [service callMyData:self action:@selector(callMyDataHandler:) dataId: 1];
    [super viewDidLoad];
}

- (void) callMyDataHandler: (id) value {

    // Handle errors
    if([value isKindOfClass:[NSError class]]) {
        NSLog(@"%@", value);
        return;
    }

    // Handle faults
    if([value isKindOfClass:[SoapFault class]]) {
        NSLog(@"%@", value);
        return;
    }               

    // Do something with the NSMutableArray* result
    NSMutableArray *result = (NSMutableArray *)value;
    MyCustomClass *myObject = [result objectAtIndex:0];
    NSLog(@"%@", myObject.myProperty); //Works Great
}

就像我说的,到目前为止一切都很完美。但我需要使用该函数之外的数据。

因此,在我的 .h 文件中,我创建了一个类似于 NSMutableArray *myDataArray; 的数组,

当我打算将结果数组复制到 myDataArray 时,它复制对象(我可以看到 myDataArray.count 值等于 result 数组),但所有对象都是“out of范围”。所以我不能使用它们。

我还尝试通过 for 循环中的索引复制所有对象,不,对象正在获取它们的值,但是当我“addObject”到 myDataArray,相同,超出范围

这里有什么问题吗?我不能用这种方式生成自定义类的数组吗?

编辑:我生成 myDataArray 的代码:

    myDataArray = [[NSMutableArray alloc] init];
    [myDataArray removeAllObjects];
    for (int i=0; i<((NSMutableArray *)result).count; i++) {
        MyCustomClass *myObject = [result objectAtIndex:i];
        [myDataArray addObject:myObject];
        [myObject release];
    }

    [self.tableView reloadData];
} //(End of callMyDataHandler function)

我之前也尝试过这种方式:

    [myDataArray removeAllObjects];
    duyurular = [result mutableCopy];
} //(End of callMyDataHandler function)

I have a SOAP service and I generated classes and functions on SudzC.com.

So I'm using the soap functions they generated, it returns an NSMutableArray with objects that are inherited by my custom class(which is generated by them, too).

So far everything's good. My values are getting into the array and I could see any property of any object with one condition: Only inside of the function that's handling the service.

Just to make it clear, here is the code:

- (void)viewDidLoad
{
    SDZGeneratedWebService* service = [SDZGeneratedWebService service];
    service.logging = YES;

    [service callMyData:self action:@selector(callMyDataHandler:) dataId: 1];
    [super viewDidLoad];
}

- (void) callMyDataHandler: (id) value {

    // Handle errors
    if([value isKindOfClass:[NSError class]]) {
        NSLog(@"%@", value);
        return;
    }

    // Handle faults
    if([value isKindOfClass:[SoapFault class]]) {
        NSLog(@"%@", value);
        return;
    }               

    // Do something with the NSMutableArray* result
    NSMutableArray *result = (NSMutableArray *)value;
    MyCustomClass *myObject = [result objectAtIndex:0];
    NSLog(@"%@", myObject.myProperty); //Works Great
}

Like I said, so far everything's perfect. But I need to use the data outside of this function.

So in my .h file, I created an array like NSMutableArray *myDataArray;

When I intend to copy the result array to myDataArray, it copies the objects(I can see that the myDataArray.count value is equal to result array's) but all the objects are "out of scope". So I cannot use them.

I also tried to copy all objects by indexes in a for loop, nope, the objects are getting their values, but when I "addObject" to myDataArray, same, out of scope.

What is wrong here? Can't I generate an array of a custom class this way?

Edit: The code I'm generating myDataArray:

    myDataArray = [[NSMutableArray alloc] init];
    [myDataArray removeAllObjects];
    for (int i=0; i<((NSMutableArray *)result).count; i++) {
        MyCustomClass *myObject = [result objectAtIndex:i];
        [myDataArray addObject:myObject];
        [myObject release];
    }

    [self.tableView reloadData];
} //(End of callMyDataHandler function)

I before tried this way, too:

    [myDataArray removeAllObjects];
    duyurular = [result mutableCopy];
} //(End of callMyDataHandler function)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回忆躺在深渊里 2024-12-15 17:22:58

您可以使用以下方法将对象从一个数组复制到另一个数组:

NSArray *source;
NSArray *dst = [[NSArray alloc] initWithArray:source];

在您的代码中,您应该删除行:[myObject release];,我最好调用[((NSMutableArray *)result) count ] 而不是使用点表示法。

You can copy objects from one array to another using this method:

NSArray *source;
NSArray *dst = [[NSArray alloc] initWithArray:source];

In your code you should remove line: [myObject release]; and I would better call [((NSMutableArray *)result) count] rather then using dot notation.

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