回答編集履歴

2

修正

2016/12/25 08:30

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -61,3 +61,39 @@
61
61
  //=> [{title "def", userCountry "456", comment "comment2"}]
62
62
 
63
63
  ```
64
+
65
+
66
+
67
+
68
+
69
+ 気になるのは質問の書き方では`return`の値が`optional型`だと思うので以下の様にしてみたらどうなるでしょうか?
70
+
71
+
72
+
73
+ ```swift
74
+
75
+ let result = hitos.filter {
76
+
77
+
78
+
79
+ if let title = $0.title, let country = $0.userCountry, let comment = $0.comment {
80
+
81
+
82
+
83
+ let title = title.contains(searchWord)
84
+
85
+ let country = country.contains(searchWord)
86
+
87
+ let comment = comment.contains(searchWord)
88
+
89
+ return title || country || comment
90
+
91
+ } else {
92
+
93
+ return false
94
+
95
+ }
96
+
97
+ }
98
+
99
+ ```

1

修正

2016/12/25 08:30

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -22,11 +22,11 @@
22
22
 
23
23
 
24
24
 
25
- let hito1 = Hito(title: "abc", userCountry: "def", comment: "comment1")
25
+ let hito1 = Hito(title: "abc", userCountry: "123", comment: "comment1")
26
26
 
27
- let hito2 = Hito(title: "hij", userCountry: "123", comment: "comment2")
27
+ let hito2 = Hito(title: "def", userCountry: "456", comment: "comment2")
28
28
 
29
- let hito3 = Hito(title: "abc", userCountry: "456", comment: "comment3")
29
+ let hito3 = Hito(title: "ghi", userCountry: "789", comment: "comment3")
30
30
 
31
31
 
32
32
 
@@ -36,7 +36,7 @@
36
36
 
37
37
 
38
38
 
39
- var searchWord = "12"
39
+ var searchWord = "45"
40
40
 
41
41
 
42
42
 
@@ -58,6 +58,6 @@
58
58
 
59
59
  result
60
60
 
61
- //=> [{title "hij", userCountry "123", comment "comment2"}]
61
+ //=> [{title "def", userCountry "456", comment "comment2"}]
62
62
 
63
63
  ```