我想知道是否有一种干净的方法可以使用Grand Central Dispatch进行倒数计时器(然后在random := rand.Reader
var key rsa.PrivateKey
loadKey("private.key",&key)
now:= time.Now()
then := now.Add(60 * 60 * 24 * 365 * 1000 * 1000 * 1000)
template:= x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: "borscht.com",
Organization: []string{"Borscht Systems AG"},
},
NotBefore:now,
NotAfter:then,
SubjectKeyId: []byte{1,2,3,4},
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
BasicConstraintsValid:true,
IsCA:true,
DNSNames:[]string{"borscht.com","localhost"},
}
derBytes,err:=x509.CreateCertificate(random, &template, &template,&key.PublicKey,&key)
if err != nil {
log.Fatal(err)
}
certCerFile,err :=os.Create("certificate.cer")
if err != nil {
log.Fatal(err)
}
certCerFile.Write(derBytes)
certCerFile.Close()
certPemFile, err := os.Create("certificate.pem")
if err != nil {
log.Fatal(err)
}
中显示)与系统时钟同步...基于参考日期? - 因此,如果我的参考日期是UILabel
,那是从现在开始的20分钟,我会在几秒钟内显示一个倒计时(不用担心格式化),它与系统时钟同步。
如果更新方法调用未按计划到达,只需快速执行此操作即可每隔一段时间跳过秒。
对我而言,这似乎是一个非常基本的问题,所以除了将更新间隔增加到10倍或者其他什么之外,我理想地寻找基本/干净的解决方案。
此外,解决方案不应使用NSDate
。
答案 0 :(得分:0)
我会使用这样的递归方法:
- (void) updateTimer{
NSDate *now = [NSDate date];
NSTimeInterval secondsToDate = [now timeIntervalSinceDate:self.referenceDate];
_timerLabel.text = [NSString stringWithFormat:@"%.0f", secondsToDate];
if( secondsToDate < 0 ){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self updateTimer];
});
}else{
NSLog(@"Timer triggered!");
}
}
你必须第一次调用它,然后它会每0.1秒更新一次倒计时。