在Swift 4.0的Firebase快照字典中获取值时,我无法解开可选的nil
这是我的代码
public class ReportDetailsActivity extends Activity {
private ArrayList<Hemoglobin> hemoglobins;
private GraphView graph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_details);
graph = findViewById(R.id.graph);
DoingNetworkStuff(Endpoints.BASE_URL+Endpoints.GET_HEMOGLOBIN_READINGS);
//Here It is working
DoTheGraphs();
}
private void DoingNetworkStuff(String url) {
final String TAG = "ReportsActivity_DoingNetworkStuff";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
hemoglobins = new ArrayList<>();
JSONArray jsonResponse = new JSONObject(response).getJSONArray("info");
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject obj = jsonResponse.getJSONObject(i);
String date = obj.optString("lappointmentdate");
String value = obj.optString("lhemoglobinreading");
hemoglobins.add(new Hemoglobin(date,value));
}
//This is where I need to call DoTheGraphs(), but not working.
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
// hide the progress dialog
pDialog.hide();
pDialog.setMessage("Check your internet connection Technical Difficulties!");
pDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
pDialog.dismiss();
}
}, 5000);
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("patientid", "VIU1111111111433");
params.put("patientdependantid", "");
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
}
private void DoTheGraphs() {
Date d1 = null;
Date d2 = null;
Date d3 = null;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
d1 = formatter.parse("03/09/1995");
d2 = formatter.parse("04/09/1995");
d3 = formatter.parse("05/09/1995");
} catch (ParseException e) {
e.printStackTrace();
}
Date[] date = {d1,d2,d3};
DataPoint[] dataPoints = new DataPoint[3];
for(int i=0;i<3;i++){
dataPoints[i]=new DataPoint(date[i],1);//new DataPoint(hemoglobins.get(i).getDate(), hemoglobins.get(i).getValue());
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dataPoints);
graph.addSeries(series);
graph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(ReportDetailsActivity.this));
graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space
graph.getViewport().setMinX(date[0].getTime());
graph.getViewport().setMaxX(date[2].getTime());
graph.getViewport().setXAxisBoundsManual(true);
graph.getGridLabelRenderer().setHumanRounding(false);
}
}
当我运行它时,它给我一个错误:
线程1:致命错误:展开包装时意外发现nil 可选值
我也在Xcode 9.0的Swift 4.0中编写此代码
任何人都可以帮我几个礼拜了
非常感谢您的帮助
答案 0 :(得分:1)
如果您需要这两个值来继续执行,我建议您使用guard
,这样您就可以使它们排成一行,并避免缩进,以便于阅读。
defer { print(self.questions.count) }
guard let dict = snapshot.value as? NSDictionary else { return }
guard let questionTitle = dict["name"] as? String else { return }
guard let created_by = dict["email"] as? String else { return }
let question = Question(questionName: questionTitle, created_by: created_by)
self.questions.append(question)
答案 1 :(得分:0)
只需将下面的代码替换为您的代码即可。
let questionTitle = dict["name"] as? String ?? ""
let created_by = dict["email"] as? String ?? ""
它将为您工作。