开发者

NSMutable Array: Getting "out of scope" Status After Mutable Copying

开发者 https://www.devze.com 2023-04-11 17:06 出处:网络
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 cl

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];
} //开发者_Go百科(End of callMyDataHandler function)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号