回答編集履歴
3
「全角のみです」というワードが誤っていたため修正しました。
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
struct ContentView: View {
|
8
8
|
@State var hoge = ""
|
9
|
-
@State var textStr = "
|
9
|
+
@State var textStr = "半角のみです"
|
10
10
|
var body: some View {
|
11
11
|
VStack {
|
12
12
|
VStack {
|
2
条件を間違っていたため修正しました。
test
CHANGED
@@ -14,22 +14,22 @@
|
|
14
14
|
}
|
15
15
|
.onChange(of: hoge) {newValue in
|
16
16
|
if isAll(hoge) {
|
17
|
-
hoge = String(hoge.prefix(
|
17
|
+
hoge = String(hoge.prefix(10))
|
18
|
-
textStr = "
|
18
|
+
textStr = "半角のみです"
|
19
19
|
}
|
20
20
|
else {
|
21
|
-
hoge = String(hoge.prefix(
|
21
|
+
hoge = String(hoge.prefix(5))
|
22
|
-
textStr = "
|
22
|
+
textStr = "全角が含まれています"
|
23
23
|
}
|
24
24
|
}
|
25
25
|
|
26
|
-
Text(textStr)
|
26
|
+
Text(textStr) //内容確認のため一時的に配置しています。
|
27
27
|
}
|
28
28
|
}
|
29
29
|
|
30
30
|
func isAll(_ target: String) -> Bool {
|
31
31
|
for str in Array(target) {
|
32
|
-
if String(str).lengthOfBytes(using: String.Encoding(rawValue: NSShiftJISStringEncoding)) =
|
32
|
+
if String(str).lengthOfBytes(using: String.Encoding(rawValue: NSShiftJISStringEncoding)) != 1 {
|
33
33
|
return false
|
34
34
|
}
|
35
35
|
}
|
1
無駄な処理を省く修正を行いました。
test
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
struct ContentView: View {
|
8
8
|
@State var hoge = ""
|
9
|
+
@State var textStr = "全角のみです"
|
9
10
|
var body: some View {
|
10
11
|
VStack {
|
11
12
|
VStack {
|
@@ -14,13 +15,15 @@
|
|
14
15
|
.onChange(of: hoge) {newValue in
|
15
16
|
if isAll(hoge) {
|
16
17
|
hoge = String(hoge.prefix(5))
|
18
|
+
textStr = "全角のみです"
|
17
19
|
}
|
18
20
|
else {
|
19
21
|
hoge = String(hoge.prefix(10))
|
22
|
+
textStr = "半角が含まれています"
|
20
23
|
}
|
21
24
|
}
|
22
25
|
|
23
|
-
|
26
|
+
Text(textStr)
|
24
27
|
}
|
25
28
|
}
|
26
29
|
|