回答編集履歴

1

引用部分の読みやすさ改善のため、Markdownを付加

2017/05/30 21:40

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -4,29 +4,35 @@
4
4
 
5
5
  [ios - How do I catch "Index out of range" in Swift? - Stack Overflow](https://stackoverflow.com/questions/37222811/how-do-i-catch-index-out-of-range-in-swift)には以下のようなコメントがありました。
6
6
 
7
+ ※読みやすさを重視して、引用部分にオリジナルのコメントに似せたMarkdownを付加しました。
7
8
 
8
9
 
9
- > Swift's Error Handling (do/try/catch) is not the solution to runtime exceptions like "index out of range".
10
+
11
+ > Swift's [Error Handling](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html) (do/try/catch) is **not** the solution to **runtime exceptions** like "index out of range".
10
12
 
11
13
  >
12
14
 
13
- > A runtime exception (you might also see these called trap, fatal error, assertion failure, etc.) is a sign of programmer error. Except in -Ounchecked builds, Swift usually guarantees that these will crash your program, rather than continuing to execute in a bad/undefined state. These sorts of crashes can arise from force-unwrapping with !, implicit unwrapping, misuse of unowned references, integer operations/conversions which overflow, fatalError()s and precondition()s and assert()s, etc. (And, unfortunately, Objective-C exceptions.)
15
+ > A runtime exception (you might also see these called __trap__, __fatal error__, __assertion failure__, etc.) is a sign of programmer error. Except in `-Ounchecked` builds, Swift usually guarantees that these will **crash** your program, rather than continuing to execute in a bad/undefined state. These sorts of crashes can arise from force-unwrapping with `!`, implicit unwrapping, misuse of `unowned` references, integer operations/conversions which overflow, `fatalError()`s and `precondition()`s and `assert()`s, etc. (And, unfortunately, Objective-C exceptions.)
14
16
 
15
17
  >
16
18
 
17
- > The solution is to simply avoid these situations. In your case, check the bounds of the array:
19
+ > The solution is to simply **avoid these situations**. In your case, check the bounds of the array:
18
20
 
19
21
  >
20
22
 
21
- > if indexPath.section < msgSections.count && indexPath.row < msgSections[indexPath.section].msg.count {
23
+ > ```
22
24
 
23
- > let msg = msgSections[indexPath.section].msg[indexPath.row]
25
+ if indexPath.section < msgSections.count && indexPath.row < msgSections[indexPath.section].msg.count {
24
26
 
25
- > // ...
27
+ let msg = msgSections[indexPath.section].msg[indexPath.row]
26
28
 
27
- > }
29
+ // ...
28
30
 
31
+ }
32
+
33
+ ```
34
+
29
- > (Or, as rmaddy says in comments — investigate why this problem is occurring! It really shouldn't happen at all.)
35
+ > (Or, as rmaddy says in comments — investigate **why** this problem is occurring! It really shouldn't happen at all.)
30
36
 
31
37
 
32
38