The error message indicates that there's a syntax issue with the foreach loop in list_view.mc. Let's take a closer look.
Error Analysis
The error is occurring at line 28, column 12, which suggests that the foreach loop syntax is not correct. Specifically, the issue is with the in keyword.
Solution
In Monkey C, the foreach loop syntax is slightly different from other languages. Instead of using the in keyword, you should use the of keyword.
Corrected Code
Replace the line:
foreach(s in _summits) {
with:
foreach (var s of _summits) {
This should fix the syntax error.
Additional Tips
- Make sure that
_summits is a collection (e.g., an array or a list) that can be iterated over using a foreach loop.
- If
_summits is not a collection, you may need to modify the code to use a different iteration method (e.g., a for loop).
Updated list_view.mc
Here's the corrected list_view.mc file:
using Toybox.WatchUi;
using Toybox.Graphics;
using Toybox.Position;
using Toybox.System;
class SotaListView extends WatchUi.ScrollableMenu {
var _summits = [];
function initialize() {
ScrollableMenu.initialize();
}
function onShow() {
// Get GPS first – will time-out after 15s
Position.enableLocationEvents(Position.LOCATION_ENABLE_TYPE_SINGLE, method(:_onFix), {
:timeout => 15000
});
}
function _onFix(info) {
if (info != null && info[:position] != null) {
var lat = info[:position][:latitude];
var lon = info[:position][:longitude];
_summits = SummitParser.getNearest([lat,lon]);
// Build the menu
var items = [];
foreach (var s of _summits) {
var dist = s.distanceTo([lat,lon]);
items.add({ :text => s.ref + " " + s.name, :subtext => dist.format("%0.1f") + " km" });
}
setMenuItems(items);
} else {
setMenuItems([{ :text=>"No GPS fix.", :subtext=>"Go outside " }]);
}
// Force refresh
WatchUi.requestUpdate();
}
/* When the user selects a summit */
function onMenuItem(menuItem) {
var idx = menuItem.getIndex();
if (idx < _summits.size()) {
return new SummitView(_summits[idx]);
}
return null;
}
}
This should fix the error and allow the code to compile correctly.