我们说我有一个名为number的变量
var number: Int = 100
如果我有一个NSTimer每秒接受一个数字变量,并且用户关闭应用程序... 20秒,当用户回到应用程序时,我怎么能得到新的数字变量它在后台运行20秒后?
答案 0 :(得分:1)
只是基于绝对时间,而不是局部重新计票。如果你需要勾选(例如更新屏幕上的计数器),那么:
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Number;
Number* oneCreator();
Number* twoCreator();
Number* threeCreator();
struct Number {
typedef Number* (*creatorFP)();
typedef map<string, creatorFP> CreatorMap;
static Number* getByName(const string &name) {
// maybe initialise this map somewhere else
CreatorMap creators;
creators.insert(make_pair(string("one"), &oneCreator));
creators.insert(make_pair(string("two"), &twoCreator));
creators.insert(make_pair(string("three"), &threeCreator));
CreatorMap::iterator creator = creators.find(name);
if (creator != creators.end()) {
return (*(creator->second))();
}
return NULL;
}
virtual void f() { cout << "NUMBER" << endl; }
};
struct One : Number {
virtual void f() { cout << "ONE" << endl; }
};
struct Two : Number {
virtual void f() { cout << "TWO" << endl; }
};
struct Three : Number {
virtual void f() { cout << "THREE" << endl; }
};
Number* oneCreator() { return new One(); }
Number* twoCreator() { return new Two(); }
Number* threeCreator() { return new Three(); }
int main() {
Number *two = Number::getByName(string("two"));
two->f();
return 0;
}