0

I have created a class NetCalculator which I am calling when a button is pressed. The method calculate network it gets 2 NSStrings and returns an id object (either "Network" object or "UIAlertView". Then I am checking which object is and I present the data. When I am using the UIAlertView the app is crashing after showing 2-3 alerts.

Any ides why this happens? On terminal it doesnt show any error just some random hexadecimal.

-(IBAction)calculate:(id)sender {
    id result;

    Network *network = [[Network alloc]init];

    NetCalculator *netCalculated = [[NetCalculator alloc] init];
    result = [netCalculated calculateNetworkWithIP:ipLabel.text andSubnet:subnetLabel.text];

    if([result isKindOfClass:[Network class]]){
        network = result;
        NSLog(@"network %@",network.networkIP);    
    }

    else if([result isKindOfClass:[UIAlertView class]]) {

        UIAlertView *alert;
        alert = result;

        [alert show];    
    }
};
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
BlackM
  • 3,927
  • 8
  • 39
  • 69

1 Answers1

0

Your code is quite strange to me. Your method calculateNetworkWithIP could return a Network result or a UIAlertView result. I wouldn't follow such an approach.

If the problem relies on memory you should show us hot that method is implemented.

Anyway, I would propose some changes (The following code does not take into account ARC or non ARC code). In particular, I would modify the calculateNetworkWithIP to return a Network result. An error will populated if a problem arises and it is passed as an argument.

- (Network*) calculateNetworkWithIP:(NSString *)ip subnet:(NSString*)subnet error:(NSError**)error 

If all is ok, the result would be be a Network and so print it or reused it somewhere. Otherwise an NSError would be returned and based on that create and show an alert view.

So, here pseudo code to do it.

NetCalculator *netCalculated = [[NetCalculator alloc] init];

NSError* error = nil;
Network* networkResult = [netCalculated calculateNetworkWithIP:ipLabel.text subnet:subnetLabel.text error:&error];

if(error != nil) {
    // create and show an alert view with the error you received
} else {
    // all ok so, for example, save the result in a instance variable
}

To follow a similar approach you can take a look at why is "error:&error" used here (objective-c).

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Your answer looks good but since I am unfamiliar with creating my own NSErrors what should my method return in case of error? The method expects Network object so I will return nil? Also how I will pass the error in the "error" variable? – BlackM Dec 29 '13 at 23:42
  • I tried returning nil and initializing the error in the NetworkCalculator by doing this: error = [[NSError alloc] initWithDomain:@"some error" code:5 userInfo:nil]; but the "error" is still nil in the main – BlackM Dec 30 '13 at 00:06
  • I found the problem. I was using NSError instead of NSError*. Thank you – BlackM Dec 30 '13 at 00:17