我有一个oc版本
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
let trueHeading = newHeading.trueHeading
let angle = Double.pi / 180 * trueHeading
let dir = [ "N","NE","E","SE","S","SW","W","NW" ]
let dir2=dir[(((trueHeading-22.5)/45.0)) as Int & 7]
}
然后我转换为swift版本
let dir2=dir[(((trueHeading-22.5)/45.0)) as Int & 7]
但它不起作用,有错误"预期','分离器"在这一行
<table id="post-table" class="table display-table table-striped table-
bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Class</th>
<th>Published</th>
<th>Quiz</th>
<th>Create Quiz</th>
<th>View Quiz</th>
</tr>
</thead>
<tbody>
<?php foreach ($allposts as $allpost) { ?>
<tr>
<td><?php echo $grades->getGradename($allpost['grade_id']); ?></td>
<td><?php echo $others->milliToTime($allpost['created_at']); ?></td>
<td><?php echo $quiz->quizCount($allpost['post_id']); ?></td>
<td>
<a href="#" data-post-id='<?php echo $allpost['post_id']; ?>' class="create-quiz"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
</td>
<td><a href="/admin/quiz/allquiz.php?post_id=<?php echo $allpost['post_id'] ?>"><i class="fa fa-eye" aria-hidden="true"></i></a></td>
</tr>
<?php } ?>
</tbody>
</table>
答案 0 :(得分:3)
你想要:
func compassDirection(for heading: CLLocationDirection) -> String? {
if heading < 0 { return nil }
let directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
let index = Int((heading + 22.5) / 45.0) & 7
return directions[index]
}
注意,它是+
,而不是-
。
或者您可以使用rounded
来避免混淆是否添加或减去22.5:
let index = Int((heading / 45).rounded()) % 8
然后您可以使用此compassDirection
结果:
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
guard let direction = compassDirection(for: newHeading.trueHeading) else {
// handle lack of heading here
return
}
// you can use `direction` here
}
答案 1 :(得分:2)
试试这个let dir2=dir[Int((trueHeading - 22.5) / 45.0) & 7]