質問編集履歴

1

内容を詳しく修正

2016/10/13 07:06

投稿

ryukku3555
ryukku3555

スコア13

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,137 @@
7
7
  1つの.swiftに色々なデータ(変数や定数)のみまとめておき、
8
8
 
9
9
  他の.swiftから呼び出していつでも利用できるようにすることは可能でしょうか?
10
+
11
+
12
+
13
+ 以下fromageblancさんから頂いたサンプルコードを元に作成した
14
+
15
+ ```
16
+
17
+ Ex.swift
18
+
19
+
20
+
21
+ import UIKit
22
+
23
+
24
+
25
+ class Ex: NSObject {
26
+
27
+
28
+
29
+ // global
30
+
31
+ var globalValue1:String = "global 1"
32
+
33
+ var globalValue2:String = "global 2"
34
+
35
+
36
+
37
+ class Ex {
38
+
39
+
40
+
41
+ // static
42
+
43
+ static var staticValue1:String = "static 1"
44
+
45
+ static var staticValue2:String = "static 2"
46
+
47
+
48
+
49
+ // class
50
+
51
+ class var classValue1:String {
52
+
53
+ return "class 1"
54
+
55
+ }
56
+
57
+
58
+
59
+ class var classValue2:String {
60
+
61
+ return "class 2"
62
+
63
+ }
64
+
65
+ }
66
+
67
+
68
+
69
+ }
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ ViewController.swift
78
+
79
+
80
+
81
+ import UIKit
82
+
83
+
84
+
85
+ class ViewController: UIViewController {
86
+
87
+
88
+
89
+ override func viewDidLoad() {
90
+
91
+ super.viewDidLoad()
92
+
93
+ // Do any additional setup after loading the view, typically from a nib.
94
+
95
+
96
+
97
+ print(Ex.classValue1) // class 1
98
+
99
+ print(Ex.classValue2) // class 2
100
+
101
+ print(Ex.staticValue1) // static 1
102
+
103
+ print(Ex.staticValue2) // static 2
104
+
105
+ print(globalValue1) // global 1
106
+
107
+ print(globalValue2) // global 2
108
+
109
+ }
110
+
111
+
112
+
113
+ override func didReceiveMemoryWarning() {
114
+
115
+ super.didReceiveMemoryWarning()
116
+
117
+ // Dispose of any resources that can be recreated.
118
+
119
+ }
120
+
121
+
122
+
123
+
124
+
125
+ }
126
+
127
+
128
+
129
+ ```
130
+
131
+ // Ex.swiftファイル内の変数にアクセス
132
+
133
+ print(Ex.classValue1) エラー type "Ex" has no member "classValue1"
134
+
135
+ print(Ex.classValue2) エラー type "Ex" has no member "classValue2"
136
+
137
+ print(Ex.staticValue1) エラー type "Ex" has no member "staticValue1"
138
+
139
+ print(Ex.staticValue2) エラー type "Ex" has no member "staticValue2"
140
+
141
+ print(globalValue1) エラー use of undeclared identifier"globalValue1"
142
+
143
+ print(globalValue2) エラー use of undeclared identifier"globalValue2"