Ios 变量在方法结束时被指定为默认值为什么?
我已经声明了两个全局变量inter,exter,它们在if或else条件下存储值,但当函数结束时,它的值show=0(这是初始化的默认值),但当我在function else语句中使用NSLog时,它会显示正确的赋值=1或2。还尝试使用NSString,但该问题分配给Null。代码如下,请查看:Ios 变量在方法结束时被指定为默认值为什么?,ios,objective-c,xcode,Ios,Objective C,Xcode,我已经声明了两个全局变量inter,exter,它们在if或else条件下存储值,但当函数结束时,它的值show=0(这是初始化的默认值),但当我在function else语句中使用NSLog时,它会显示正确的赋值=1或2。还尝试使用NSString,但该问题分配给Null。代码如下,请查看: -(void)getFutureAppFrame{ reachability = [Reachability reachabilityForInternetConnection];
-(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);
}
-(void)getFutureAppFrame{
可达性=[reachability reachabilityForInternetConnection];
[可达性启动通知];
NetworkStatus remoteHostStatus=[reachability currentReachabilityStatus];
NSString*phyIdTemp=[[NSUserDefaults standardUserDefaults]objectForKey:@“phyId”];
NSMutableString*urlStr=[NSMutableString stringWithFormat:getFutureApptURL,[[NSUserDefaults standardUserDefaults]objectForKey:@“URL”],phyIdTemp,PatientID];
NSString*encodedUrl=[urlStr STRINGBY添加百分比转义使用编码:
NSASCIIStringEncoding];
NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:encodedUrl]缓存策略:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[NSURLConnection sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue]完成处理程序:^(NSURLResponse*响应,NSData*数据,NSError*错误){
n错误*错误1;
NSMutableDictionary*dict=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:&error1];
NSMutableArray*futApptInt=[[dict objectForKey:@“获取约会”]objectForKey:@“内部”];
如果(futApptInt.count在网络操作完成之前,inter
,exter
的值没有设置,这是NSLog()之后的
语句,因为它在异步块之外。这是异步操作的本质,什么解决方案对它来说是新的..谢谢Reply@iDeepak,您需要等待异步块完成,然后只有您才能获得exter&inter的值。@pawan感谢您的回复,asper i测试在不同点添加了nslog在uIButton to Viewdidload中,视图将出现,但不在其中分配值只是默认值。您是否可以引导我,我需要一个新功能,或者我可以在这里执行此操作function@iDeepak您可以创建一个新函数,它将在块完成后被调用,在那里您将得到更新的值。