質問編集履歴
1
内容を詳しく修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,4 +2,71 @@
|
|
2
2
|
swift初心者です。
|
3
3
|
|
4
4
|
1つの.swiftに色々なデータ(変数や定数)のみまとめておき、
|
5
|
-
他の.swiftから呼び出していつでも利用できるようにすることは可能でしょうか?
|
5
|
+
他の.swiftから呼び出していつでも利用できるようにすることは可能でしょうか?
|
6
|
+
|
7
|
+
以下fromageblancさんから頂いたサンプルコードを元に作成した
|
8
|
+
```
|
9
|
+
Ex.swift
|
10
|
+
|
11
|
+
import UIKit
|
12
|
+
|
13
|
+
class Ex: NSObject {
|
14
|
+
|
15
|
+
// global
|
16
|
+
var globalValue1:String = "global 1"
|
17
|
+
var globalValue2:String = "global 2"
|
18
|
+
|
19
|
+
class Ex {
|
20
|
+
|
21
|
+
// static
|
22
|
+
static var staticValue1:String = "static 1"
|
23
|
+
static var staticValue2:String = "static 2"
|
24
|
+
|
25
|
+
// class
|
26
|
+
class var classValue1:String {
|
27
|
+
return "class 1"
|
28
|
+
}
|
29
|
+
|
30
|
+
class var classValue2:String {
|
31
|
+
return "class 2"
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
ViewController.swift
|
40
|
+
|
41
|
+
import UIKit
|
42
|
+
|
43
|
+
class ViewController: UIViewController {
|
44
|
+
|
45
|
+
override func viewDidLoad() {
|
46
|
+
super.viewDidLoad()
|
47
|
+
// Do any additional setup after loading the view, typically from a nib.
|
48
|
+
|
49
|
+
print(Ex.classValue1) // class 1
|
50
|
+
print(Ex.classValue2) // class 2
|
51
|
+
print(Ex.staticValue1) // static 1
|
52
|
+
print(Ex.staticValue2) // static 2
|
53
|
+
print(globalValue1) // global 1
|
54
|
+
print(globalValue2) // global 2
|
55
|
+
}
|
56
|
+
|
57
|
+
override func didReceiveMemoryWarning() {
|
58
|
+
super.didReceiveMemoryWarning()
|
59
|
+
// Dispose of any resources that can be recreated.
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
```
|
66
|
+
// Ex.swiftファイル内の変数にアクセス
|
67
|
+
print(Ex.classValue1) エラー type "Ex" has no member "classValue1"
|
68
|
+
print(Ex.classValue2) エラー type "Ex" has no member "classValue2"
|
69
|
+
print(Ex.staticValue1) エラー type "Ex" has no member "staticValue1"
|
70
|
+
print(Ex.staticValue2) エラー type "Ex" has no member "staticValue2"
|
71
|
+
print(globalValue1) エラー use of undeclared identifier"globalValue1"
|
72
|
+
print(globalValue2) エラー use of undeclared identifier"globalValue2"
|