質問編集履歴

4

利用できない変数に書き直してしまっていたため修正、エラー文言も違ったので修正

2019/10/30 04:25

投稿

carpbream
carpbream

スコア32

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  ### エラー内容
22
22
 
23
- > 'Item' is not convertible to 'A.Type' (aka 'Item')
23
+ > Cannot convert value of type '(Item?) -> ()' to expected argument type '(A.T?) -> Void' (aka '(Optional<Item>) -> ()')
24
24
 
25
25
 
26
26
 
@@ -32,9 +32,9 @@
32
32
 
33
33
  protocol P {
34
34
 
35
- associatedtype Type: Codable
35
+ associatedtype T: Codable
36
36
 
37
- static func get(request: Request, completion: @escaping(_ data: Type?) -> Void)
37
+ static func get(request: Request, completion: @escaping(_ data: T?) -> Void)
38
38
 
39
39
  }
40
40
 
@@ -42,13 +42,13 @@
42
42
 
43
43
  extension P {
44
44
 
45
- static func get(request: Request, completion: @escaping(_ data: Type?) -> Void) {
45
+ static func get(request: Request, completion: @escaping(_ data: T?) -> Void) {
46
46
 
47
47
  let jsonData = getSomeJsonData()
48
48
 
49
49
  do {
50
50
 
51
- let data = try JSONDecoder().decode(Type.self, from: jsonData)
51
+ let data = try JSONDecoder().decode(T.self, from: jsonData)
52
52
 
53
53
  completion(data)
54
54
 
@@ -68,7 +68,7 @@
68
68
 
69
69
  class A: P {
70
70
 
71
- typealias Type = Item // ItemはCodableなstruct
71
+ typealias T = Item // ItemはCodableなstruct
72
72
 
73
73
  }
74
74
 
@@ -80,7 +80,7 @@
80
80
 
81
81
  func printItem() {
82
82
 
83
- A.get(request: request) { (data: Item?) in // エラー! 'Item' is not convertible to 'A.Type' (aka 'Item')
83
+ A.get(request: request) { (data: Item?) in // エラー! Cannot convert value of type '(Item?) -> ()' to expected argument type '(A.T?) -> Void' (aka '(Optional<Item>) -> ()')
84
84
 
85
85
  print(data)
86
86
 

3

実際の変数名が入ってしまっていたので修正

2019/10/30 04:25

投稿

carpbream
carpbream

スコア32

test CHANGED
File without changes
test CHANGED
@@ -44,9 +44,11 @@
44
44
 
45
45
  static func get(request: Request, completion: @escaping(_ data: Type?) -> Void) {
46
46
 
47
+ let jsonData = getSomeJsonData()
48
+
47
49
  do {
48
50
 
49
- let data = try JSONDecoder().decode(ResultDataType.self, from: jsonData)
51
+ let data = try JSONDecoder().decode(Type.self, from: jsonData)
50
52
 
51
53
  completion(data)
52
54
 

2

extension P の中のメソッドが実態に即していなかったため修正

2019/10/30 04:04

投稿

carpbream
carpbream

スコア32

test CHANGED
File without changes
test CHANGED
@@ -44,9 +44,19 @@
44
44
 
45
45
  static func get(request: Request, completion: @escaping(_ data: Type?) -> Void) {
46
46
 
47
- let item = Item()
47
+ do {
48
48
 
49
+ let data = try JSONDecoder().decode(ResultDataType.self, from: jsonData)
50
+
49
- completion(item)
51
+ completion(data)
52
+
53
+ } catch {
54
+
55
+ print(error.localizedDescription)
56
+
57
+ completion(nil)
58
+
59
+ }
50
60
 
51
61
  }
52
62
 
@@ -66,7 +76,7 @@
66
76
 
67
77
  class B {
68
78
 
69
- func getItem() {
79
+ func printItem() {
70
80
 
71
81
  A.get(request: request) { (data: Item?) in // エラー! 'Item' is not convertible to 'A.Type' (aka 'Item')
72
82
 

1

実際の実装と差異がありました。

2019/10/30 04:03

投稿

carpbream
carpbream

スコア32

test CHANGED
File without changes
test CHANGED
@@ -40,17 +40,23 @@
40
40
 
41
41
 
42
42
 
43
- class A: P {
43
+ extension P {
44
44
 
45
- typealias Type = Item // ItemはCodableなstruct
46
-
47
- static func get(request: Request, completion: @escaping(_ data: Item?) -> Void) {
45
+ static func get(request: Request, completion: @escaping(_ data: Type?) -> Void) {
48
46
 
49
47
  let item = Item()
50
48
 
51
49
  completion(item)
52
50
 
53
51
  }
52
+
53
+ }
54
+
55
+
56
+
57
+ class A: P {
58
+
59
+ typealias Type = Item // ItemはCodableなstruct
54
60
 
55
61
  }
56
62