如何将NSTimeInterval转换为整数值?
我的TimeInterval保存值83.01837
。我需要将其转换为83
。我用谷歌搜索但找不到任何帮助。
答案 0 :(得分:62)
直接分配:
NSTimeInterval interval = 1002343.5432542;
NSInteger time = interval;
//time is now equal to 1002343
NSTimeInterval是一个double,所以如果你直接将它分配给一个NSInteger(或int,如果你愿意的话)它就可以了。这将把时间缩短到最接近的秒数。
如果您希望舍入到最接近的秒(而不是将其切断),您可以在作出任务前使用回合:
NSTimeInterval interval = 1002343.5432542;
NSInteger time = round(interval);
//time is now equal to 1002344
答案 1 :(得分:8)
根据the documentation,NSTimeInterval
只是double
:
typedef double NSTimeInterval;
您可以将其转换为int
:
seconds = (int) myTimeInterval;
注意溢出!
答案 2 :(得分:7)
在Swift 3.0中
public class main extends ListActivity implements android.widget.CompoundButton.OnCheckedChangeListener {
SQLiteDatabase dBase;
private CheckBox checkBox1
ListView lv;
ArrayList<Student>studentList;
StudentAdapter slAdapter;
private Context context;
String returnString;
private String URL_ITEMS;
private String section;
ProgressDialog progressdialog;
private static final String TAG_FIXTURE = "fixture";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
JSONArray matchFixture = null;// to store the result of MySQL query after decoding JSON
ArrayList<HashMap<String, String>> matchFixtureList = new ArrayList<HashMap<String, String>>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// GET THE DATA FROM OLD INTENT
Intent previous = getIntent();
section = previous.getStringExtra("section");
Log.i("Section: ", " " + section);
// Now generate the URL
URL_ITEMS = "http://10.0.2.2/attend/getFixture.php?section=" + section;
new GetFixture().execute();
}
// declare parameters that are passed to PHP script i.e. the name "section" and its value submitted by user
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
private class GetFixture extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(main.this);
progressdialog.setTitle("Please Wait");
progressdialog.setMessage("Getting Information from Server");
progressdialog.setCancelable(false);
progressdialog.show();
}
@Override
protected Void doInBackground(Void... arg) {
ServiceHandler serviceClient = new ServiceHandler();
Log.d("url: ", "> " + URL_ITEMS);
String json = serviceClient.makeServiceCall(URL_ITEMS,ServiceHandler.GET);
// print the json response in the log
Log.d("main.class", " Get match fixture response: > " + json);
if (json != null) {
try {
Log.d("try", "in the try");
JSONObject jsonObj = new JSONObject(json);
Log.d("jsonObject", "new json Object");
// Getting JSON Array node
matchFixture = jsonObj.getJSONArray(TAG_FIXTURE);
Log.d("json aray", "user point array");
int len = matchFixture.length();
Log.d("len", "get array length");
if(matchFixture.length() == 0) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(main.this, "No Data Available for Section " + section, Toast.LENGTH_SHORT).show();
}
});
}
for (int i = 0; i < matchFixture.length(); i++) {
JSONObject c = matchFixture.getJSONObject(i);
String id = c.getString(TAG_ID);
Log.d("id", id);
String name = c.getString(TAG_NAME);
Log.d("name", name);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_ID, id);
matchFixture.put(TAG_NAME, name);
matchFixtureList.add(matchFixture);
}
}
catch (JSONException e) {
Log.d("catch", "in the catch");
e.printStackTrace();
}
}
else {
Log.e("JSON Data", "Didn't receive any data from server!");
Toast.makeText(main.this, "Check Internet Connection", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
main.this, matchFixtureList,
R.layout.list1_item, new String[] {
TAG_ID, TAG_NAME }
, new int[] {
R.id.id,R.id.name
}
);
setListAdapter(adapter);
// lv.setAdapter(adapter);
//lv.setAdapter(slAdapter);
if(progressdialog.isShowing()) {
progressdialog.dismiss();
}
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(main.this, "into the on checked change listener..", Toast.LENGTH_SHORT).show();
int pos = lv.getPositionForView(buttonView);
if(pos !=ListView.INVALID_POSITION){
for (int i = 0; i < matchFixture.length(); i++) {
JSONObject c = null;
try {
c = matchFixture.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = null;
try {
id = c.getString(TAG_ID);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = null;
try {
name = c.getString(TAG_NAME);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
database.execSQL("CREATE TABLE IF NOT EXISTS attendance(id integer primary key autoincrement,name VARCHAR(150),id INT(10)");
database.execSQL("INSERT INTO attendance(name,id) VALUES('" + name + "'," + id+");");
Toast.makeText(main.this, "record inserted..", Toast.LENGTH_SHORT).show();
database.close();
}
}
}
/** You can not insert empty values and you should create Database and tables first **/
//ContentValues values= new ContentValues();
// dBase.insert("attendance", null, values);
// Toast.makeText(getApplicationContext(), "Record Inserted..", 2000).show();
答案 3 :(得分:6)
我怀疑来自NSDate的NSTimeInterval值会溢出NSInteger。你可能想要很长时间。 (64位整数。)那些可以存储鸣喇叭大整数值(-2 ^ 63到2 ^ 63 -1)
long long integerSeconds = round([NSDate timeIntervalSinceReferenceDate]);
看起来像NSInteger CAN 存储NSTimeInterval,至少在接下来的几十年里。当前日期的timeIntervalSinceReferenceDate大约是519,600,000,或大约2 ^ 28。在32位设备上,NSInteger可以保存从-2 ^ 31到2 ^ 31-1的值。 (2 ^ 31是2,147,483,648
答案 4 :(得分:0)
Swift 4,Swift 5
我只是强制转换为Int64
:
Int64(Date().timeIntervalSince1970)
答案 5 :(得分:-1)
我需要在Swift Number中存储NSDate。我使用了以下演员表,它很棒。
Double(startDateTime.timeIntervalSince1970)