变量在方法结束时分配给默认值为什么?

时间:2015-09-01 03:29:25

标签: ios objective-c xcode

我已经声明了两个全局变量inter,在if或else条件下exter那些存储值,但是当函数结束时它显示值show = 0(默认为初始化)但是当我在函数Else Statement中使用NSLog时这显示正确分配value = 1或2。也尝试使用NSString,但同样的问题,这个分配给Null ..Code在下面看看:

-(void)getFutureAppFrame{


    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    NSString  *phyIdTemp=[[NSUserDefaults standardUserDefaults]  objectForKey:@"phyId"];

    NSMutableString *  urlStr=  [NSMutableString stringWithFormat:getFutureApptURL,[[NSUserDefaults standardUserDefaults]objectForKey:@"URL"],phyIdTemp, PatientID];
    NSString* encodedUrl = [urlStr stringByAddingPercentEscapesUsingEncoding:
                            NSASCIIStringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:encodedUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {

        NSError* error1;
        NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error1];



        NSMutableArray *futApptInt = [[dict objectForKey:@"get_appointments"] objectForKey:@"internal"];
        if(futApptInt.count <= 0)
        {
            inter = 1;
        }
        else{
            inter = 2;
        }
        // return;
        futApptInt = [[dict objectForKey:@"get_appointments"] objectForKey:@"external"];
        if(futApptInt.count <= 0)
        {
            exter =1;
        }
        else{
            exter = 2;
        }
        NSLog(@"CheckELSE %d --%d",exter , inter);

    }];
    NSLog(@"CHECKF %d --%d",exter , inter);

}

1 个答案:

答案 0 :(得分:2)

interexter的值在网络操作完成后才会设置,> NSLog()语句后,因为它在异步块之外。这是异步操作的本质,