简体   繁体   English

目标C整数第一次是0,但是第二次改变

[英]Objective C Integer is 0 first time, but it changes second time

Okay so i have this program that is suposed to communicate over a Webserver(PHP Script) 好的,所以我有这个程序可以通过Web服务器进行通信(PHP脚本)

I have this method that i want to get a random id for a table. 我有这种方法,我想获得一个表的随机ID。 The return data of this NSURLconnection is supose to be the amount of rows in the table. 该NSURLconnection的返回数据被假定为表中的行数。

First time i call it, its like randomid variables does not get set, but second time i run it, it does get set. 第一次调用它,它的像randomid变量没有被设置,但是第二次我运行它,它被设置了。

variable setting: 变量设置:

@implementation ViewController {
NSMutableData *randomData;
int randomid;
}

Here is my IBaction for a button: 这是我的按钮操作:

- (IBAction)initVits:(id)sender {
[self randomID];
NSLog(@"Random ID: %d", randomid);    
}

The output first time is: Random ID: 0, second time: Random ID: a valid id 第一次输出是:随机ID:0,第二次:随机ID:有效ID

RandomID: RandomID:

- (void) randomID {

NSString *url = @"http://ivitserdk.porkhost.dk/ivitserdk.php?function=randomid";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]   cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1.0];
connectionRandomID = [[NSURLConnection alloc] initWithRequest:request delegate:self];

randomData = [NSMutableData data];
[connectionRandomID start];
}

Connection did finish loading: 连接确实完成加载:

 if(connection == connectionRandomID) {
    NSString *String = [[NSString alloc] initWithData:randomData encoding:NSUTF8StringEncoding];
    NSLog(@"Return: %@", String);
    int temp = [String integerValue];
    NSLog(@"temp: %d", temp);
    randomid = (arc4random() % temp) + 1;
    NSLog(@"Random: %d", randomid);
    randomData = nil;
    connectionRandomID = nil;
 }

Output is for all times: Return: (number of rows in table) temp: (number of rows in table to a int) Random: (a random number) 输出始终为:返回:(表中的行数)temp:(表中的int的行数)随机数:(随机数)

Its like the randomid variable does not get set the first time. 就像第一次没有设置randomid变量一样。

Thanks for your time! 谢谢你的时间!

[self randomID] returns immediatly because you are doing an async call inside randomID function. [self randomID]立即返回,因为您正在randomID函数内进行异步调用。

randomid is set on the didFinish callback that it is executed after you NSLog the ivar randomid. 在NSLog ivar randomid之后执行的didFinish回调上设置了randomid。

You are using asynchronous networking, so your NSLog is called before the random ID is retrieved from the server. 您正在使用异步网络,因此在从服务器检索随机ID之前将调用NSLog There are multiple solutions to this. 有多种解决方案。

  • Use synchronous networking (I don't recommend this, so I don't go further into that) 使用同步网络(我不建议这样做,因此我不做进一步介绍)
  • Put the NSLog statement or whatever you want to do when the ID is generated at the end of your connectionDidFinishLoading method connectionDidFinishLoading方法末尾生成ID时,放入NSLog语句或您要执行的操作
  • If possible, I'd recommend generating the ID on the device (but I guess you need an unique key for a database, an URL or something, so this will probably not work for you) 如果可能,我建议在设备上生成ID(但我想您需要数据库,URL或其他内容的唯一密钥,因此这可能对您不起作用)

Did you add this ? 您添加了吗?

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[randomData appendData:data];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM