質問編集履歴

3

エラーメッセージの追加

2015/12/04 01:54

投稿

mottyan23
mottyan23

スコア13

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,10 @@
9
9
  ###発生している問題・エラーメッセージ
10
10
 
11
11
  extensionでクラスを拡張する時にエラーが出る.
12
+
13
+
14
+
15
+ エラーメッセージ:Declaration is only valid at file scope
12
16
 
13
17
 
14
18
 
@@ -130,7 +134,7 @@
130
134
 
131
135
 
132
136
 
133
- まだプログラミングを始めたばかりで何もわかっていませんが、何かアプリを作りたいと思い、色判別を行ってみようと思いました。
137
+ まだプログラミングを始めたばかりで何もわかっていませんが、何かアプリを作りたいと思い、カメラを用いた色判別を行ってみようと思いました。
134
138
 
135
139
 
136
140
 

2

ソースコードの修正

2015/12/04 01:54

投稿

mottyan23
mottyan23

スコア13

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,8 @@
15
15
  ###ソースコード
16
16
 
17
17
  ```swift
18
+
19
+
18
20
 
19
21
  import UIKit
20
22
 
@@ -40,83 +42,61 @@
40
42
 
41
43
  let imageData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
42
44
 
43
- let data : UnsafePointer = CFDataGetBytePtr(imageData) let scale = UIScreen.mainScreen().scale
45
+ let data : UnsafePointer = CFDataGetBytePtr(imageData)
44
46
 
45
- let address : Int = ((Int(self.size.width) * Int(pos.y * scale)) + Int(pos.x * scale)) * pixelDataByteSize
47
+ let scale = UIScreen.mainScreen().scale
46
48
 
47
- let r = CGFloat(data[address])
49
+ let address : Int = ((Int(self.size.width) * Int(pos.y * scale)) + Int(pos.x * scale)) * pixelDataByteSize
48
50
 
49
- let g = CGFloat(data[address+1])
51
+ let r = CGFloat(data[address])
50
52
 
51
- let b = CGFloat(data[address+2])
53
+ let g = CGFloat(data[address+1])
52
54
 
55
+ let b = CGFloat(data[address+2])
56
+
53
- let a = CGFloat(data[address+3])
57
+ let a = CGFloat(data[address+3])
54
58
 
55
59
 
56
60
 
57
- return UIColor(red: r, green: g, blue: b, alpha: a)
61
+ return UIColor(red: r, green: g, blue: b, alpha: a)
58
62
 
59
- }
63
+ }
60
64
 
61
- }
65
+ }
62
66
 
63
67
  override func viewDidLoad() {
64
68
 
65
- super.viewDidLoad()
69
+ super.viewDidLoad()
66
70
 
67
71
 
68
72
 
69
-
73
+ // 画面の背景色をグレーにする
70
74
 
71
-
72
-
73
- // 画面の背景色をグレーにする
74
-
75
- self.view.backgroundColor = UIColor.lightGrayColor()
75
+ self.view.backgroundColor = UIColor.lightGrayColor()
76
76
 
77
77
 
78
78
 
79
-
79
+ // イメージビューを作る
80
80
 
81
+ let rect = CGRect(x: 0, y: 0, width: 300, height: 200)
81
82
 
83
+ let imageView = UIImageView(frame: rect)
82
84
 
83
- // イメージビュを作る
85
+ // イメージの表示モ
84
86
 
85
- let rect = CGRect(x: 0, y: 0, width: 300, height: 200)
87
+ imageView.contentMode = .ScaleAspectFit
86
88
 
87
- let imageView = UIImageView(frame: rect)
89
+ // イメージビューにイメージを設定する
88
90
 
89
-
91
+ imageView.image = UIImage(named: "sheep.jpg")
90
92
 
93
+ // イメージビューの座標をルートビューの中央に設定する
91
94
 
95
+ imageView.center = self.view.center
92
96
 
93
- // イメージ表示モード
97
+ // イメージビューをルートビューに追加(表示)する
94
98
 
95
- imageView.contentMode = .ScaleAspectFit
96
-
97
-
98
-
99
-
100
-
101
- // イメージビューにイメージを設定する
102
-
103
- imageView.image = UIImage(named: "sheep.jpg")
104
-
105
-
106
-
107
-
108
-
109
- // イメージビューの座標をルートビューの中央に設定する
110
-
111
- imageView.center = self.view.center
112
-
113
-
114
-
115
-
116
-
117
- // イメージビューをルートビューに追加(表示)する
118
-
119
- self.view.addSubview(imageView)
99
+ self.view.addSubview(imageView)
120
100
 
121
101
  }
122
102
 
@@ -124,17 +104,9 @@
124
104
 
125
105
  override func didReceiveMemoryWarning() {
126
106
 
127
- super.didReceiveMemoryWarning()
107
+ super.didReceiveMemoryWarning()
128
108
 
129
-
130
-
131
-
132
-
133
- // Dispose of any resources that can be recreated.
109
+ // Dispose of any resources that can be recreated.
134
-
135
-
136
-
137
-
138
110
 
139
111
  }
140
112
 
@@ -142,13 +114,13 @@
142
114
 
143
115
 
144
116
 
145
- }
117
+ }
146
118
 
147
119
 
148
120
 
149
- }
150
121
 
122
+
151
- }```
123
+ ```
152
124
 
153
125
 
154
126
 

1

コードのインデントを揃えました

2015/12/03 22:51

投稿

mottyan23
mottyan23

スコア13

test CHANGED
@@ -1 +1 @@
1
- extensionを使用してクラスを拡張したい
1
+ [swift2]extensionを使用してクラスを拡張したい
test CHANGED
@@ -40,61 +40,83 @@
40
40
 
41
41
  let imageData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
42
42
 
43
- let data : UnsafePointer = CFDataGetBytePtr(imageData)
43
+ let data : UnsafePointer = CFDataGetBytePtr(imageData) let scale = UIScreen.mainScreen().scale
44
44
 
45
- let scale = UIScreen.mainScreen().scale
45
+ let address : Int = ((Int(self.size.width) * Int(pos.y * scale)) + Int(pos.x * scale)) * pixelDataByteSize
46
46
 
47
- let address : Int = ((Int(self.size.width) * Int(pos.y * scale)) + Int(pos.x * scale)) * pixelDataByteSize
47
+ let r = CGFloat(data[address])
48
48
 
49
- let r = CGFloat(data[address])
49
+ let g = CGFloat(data[address+1])
50
50
 
51
- let g = CGFloat(data[address+1])
51
+ let b = CGFloat(data[address+2])
52
52
 
53
- let b = CGFloat(data[address+2])
54
-
55
- let a = CGFloat(data[address+3])
53
+ let a = CGFloat(data[address+3])
56
54
 
57
55
 
58
56
 
59
- return UIColor(red: r, green: g, blue: b, alpha: a)
57
+ return UIColor(red: r, green: g, blue: b, alpha: a)
60
58
 
61
- }
59
+ }
62
60
 
63
- }
61
+ }
64
62
 
65
63
  override func viewDidLoad() {
66
64
 
67
- super.viewDidLoad()
65
+ super.viewDidLoad()
68
66
 
69
67
 
70
68
 
71
- // 画面の背景色をグレーにする
69
+
72
70
 
71
+
72
+
73
+ // 画面の背景色をグレーにする
74
+
73
- self.view.backgroundColor = UIColor.lightGrayColor()
75
+ self.view.backgroundColor = UIColor.lightGrayColor()
74
76
 
75
77
 
76
78
 
77
- // イメージビューを作る
79
+
78
80
 
79
- let rect = CGRect(x: 0, y: 0, width: 300, height: 200)
80
81
 
81
- let imageView = UIImageView(frame: rect)
82
82
 
83
- // イメージの表示モ
83
+ // イメージビュを作る
84
84
 
85
- imageView.contentMode = .ScaleAspectFit
85
+ let rect = CGRect(x: 0, y: 0, width: 300, height: 200)
86
86
 
87
- // イメージビューにイメージを設定する
87
+ let imageView = UIImageView(frame: rect)
88
88
 
89
- imageView.image = UIImage(named: "sheep.jpg")
89
+
90
90
 
91
- // イメージビューの座標をルートビューの中央に設定する
92
91
 
93
- imageView.center = self.view.center
94
92
 
95
- // イメージビューをルートビューに追加(表示)する
93
+ // イメージ表示モード
96
94
 
95
+ imageView.contentMode = .ScaleAspectFit
96
+
97
+
98
+
99
+
100
+
101
+ // イメージビューにイメージを設定する
102
+
103
+ imageView.image = UIImage(named: "sheep.jpg")
104
+
105
+
106
+
107
+
108
+
109
+ // イメージビューの座標をルートビューの中央に設定する
110
+
111
+ imageView.center = self.view.center
112
+
113
+
114
+
115
+
116
+
117
+ // イメージビューをルートビューに追加(表示)する
118
+
97
- self.view.addSubview(imageView)
119
+ self.view.addSubview(imageView)
98
120
 
99
121
  }
100
122
 
@@ -102,13 +124,25 @@
102
124
 
103
125
  override func didReceiveMemoryWarning() {
104
126
 
105
- super.didReceiveMemoryWarning()
127
+ super.didReceiveMemoryWarning()
106
128
 
129
+
130
+
131
+
132
+
107
- // Dispose of any resources that can be recreated.
133
+ // Dispose of any resources that can be recreated.
134
+
135
+
136
+
137
+
108
138
 
109
139
  }
110
140
 
111
141
 
142
+
143
+
144
+
145
+ }
112
146
 
113
147
 
114
148