回答編集履歴
15
間違えを修正します
test
CHANGED
@@ -120,6 +120,10 @@
|
|
120
120
|
|
121
121
|
|
122
122
|
|
123
|
+
import UIKit
|
124
|
+
|
125
|
+
|
126
|
+
|
123
127
|
extension Date {
|
124
128
|
|
125
129
|
// ????修正前 -> NSDate
|
@@ -162,14 +166,6 @@
|
|
162
166
|
|
163
167
|
|
164
168
|
|
165
|
-
//修正前return calendar.date(byAdding: dateComponents, to: self as Date, option: NSCalendar.Options(rawValue: 0))!
|
166
|
-
|
167
|
-
//修正前return calendar.date(byAdding: dateComponents, to: self as Date)!
|
168
|
-
|
169
|
-
//参考サイト : http://stackoverflow.com/questions/24741038/creating-a-future-date-in-swift-with-nsdate
|
170
|
-
|
171
|
-
//回答Claus
|
172
|
-
|
173
169
|
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
174
170
|
|
175
171
|
return calendar.date(byAdding: dateComponents, to: self)!
|
@@ -178,171 +174,161 @@
|
|
178
174
|
|
179
175
|
}
|
180
176
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
//
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
//
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
//前
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
nu
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
co
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
retur
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
338
|
-
|
339
|
-
let formatter: DateFormatter = DateFormatter()
|
340
|
-
|
341
|
-
formatter.dateFormat = "d"
|
342
|
-
|
343
|
-
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
344
|
-
|
345
|
-
}
|
177
|
+
|
178
|
+
|
179
|
+
class DateManager: NSObject {
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
var currentMonthOfDates = [NSDate]() //表記する月の配列
|
184
|
+
|
185
|
+
// ????修正前 NSDate()
|
186
|
+
|
187
|
+
var selectedDate = Date()
|
188
|
+
|
189
|
+
let daysPerWeek: Int = 7
|
190
|
+
|
191
|
+
// ????修正前 Int!
|
192
|
+
|
193
|
+
var numberOfItems: Int! = 0 //セルの個数 nilが入らないようにする
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//月ごとのセルの数を返すメソッド
|
198
|
+
|
199
|
+
func daysAcquisition() -> Int {
|
200
|
+
|
201
|
+
// ????修正前 NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfMonth, inUnit: NSCalendarUnit.Month, forDate: firstDateOfMonth())
|
202
|
+
|
203
|
+
let rangeOfWeeks = Calendar.current.range(of: .weekOfMonth, in: .month, for: firstDateOfMonth() as Date)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
// ????修正前 rangeOfWeeks.length
|
208
|
+
|
209
|
+
let numberOfWeeks = Int((rangeOfWeeks?.count)!) //月が持つ週の数
|
210
|
+
|
211
|
+
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
212
|
+
|
213
|
+
return numberOfItems
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
//月の初日を取得
|
218
|
+
|
219
|
+
func firstDateOfMonth() -> Date {
|
220
|
+
|
221
|
+
// ????修正前 let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: selectedDate)
|
222
|
+
|
223
|
+
var components = Calendar.current.dateComponents([.year, .month, .day], from:selectedDate)
|
224
|
+
|
225
|
+
components.day = 1
|
226
|
+
|
227
|
+
// ????修正前 NSCalendar.currentCalendar().dateFromComponents(components)!
|
228
|
+
|
229
|
+
let firstDateMonth = Calendar.current.date(from: components)!
|
230
|
+
|
231
|
+
return firstDateMonth
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
// ⑴表記する日にちの取得
|
238
|
+
|
239
|
+
func dateForCellAtIndexPath(numberOfItem: Int) {
|
240
|
+
|
241
|
+
// ①「月の初日が週の何日目か」を計算する
|
242
|
+
|
243
|
+
// ????修正前 NSCalendar.currentCalendar().ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.WeekOfMonth, forDate: firstDateOfMonth())
|
244
|
+
|
245
|
+
let ordinalityOfFirstDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: firstDateOfMonth())
|
246
|
+
|
247
|
+
// ????修正前 for var i = 0; i < numberOfItems; i++ {
|
248
|
+
|
249
|
+
for i in 0 ..< numberOfItems {
|
250
|
+
|
251
|
+
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
252
|
+
|
253
|
+
var dateComponents = DateComponents()
|
254
|
+
|
255
|
+
// ????修正前 (ordinalityOfFirstDay - 1)
|
256
|
+
|
257
|
+
dateComponents.day = i - (ordinalityOfFirstDay! - 1)
|
258
|
+
|
259
|
+
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
260
|
+
|
261
|
+
// ????修正前 NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendarOptions(rawValue: 0))!
|
262
|
+
|
263
|
+
let date = Calendar.current.date(byAdding: dateComponents as DateComponents, to: firstDateOfMonth() as Date)!
|
264
|
+
|
265
|
+
// ④配列に追加
|
266
|
+
|
267
|
+
// ????修正前 (date)
|
268
|
+
|
269
|
+
currentMonthOfDates.append(date as NSDate)
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// ⑵表記の変更
|
278
|
+
|
279
|
+
// ????修正前 (indexPath: NSIndexPath)
|
280
|
+
|
281
|
+
func conversionDateFormat(indexPath: IndexPath) -> String {
|
282
|
+
|
283
|
+
// ????修正前 (numberOfItems)
|
284
|
+
|
285
|
+
dateForCellAtIndexPath(numberOfItem: numberOfItems)
|
286
|
+
|
287
|
+
// ????修正前 NSDateFormatter = NSDateFormatter()
|
288
|
+
|
289
|
+
let formatter: DateFormatter = DateFormatter()
|
290
|
+
|
291
|
+
formatter.dateFormat = "d"
|
292
|
+
|
293
|
+
// ????修正前 return formatter.stringFromDate(currentMonthOfDates[indexPath.row])
|
294
|
+
|
295
|
+
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date)
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
//前月の表示
|
302
|
+
|
303
|
+
// ????修正前 (date: NSDate) -> NSDate
|
304
|
+
|
305
|
+
func prevMonth(date: Date) -> Date {
|
306
|
+
|
307
|
+
currentMonthOfDates = []
|
308
|
+
|
309
|
+
selectedDate = date.monthAgoDate()
|
310
|
+
|
311
|
+
return selectedDate
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
//次月の表示
|
318
|
+
|
319
|
+
// ????修正前 (date: NSDate) -> NSDate
|
320
|
+
|
321
|
+
func nextMonth(date: Date) -> Date {
|
322
|
+
|
323
|
+
currentMonthOfDates = []
|
324
|
+
|
325
|
+
selectedDate = date.monthLaterDate()
|
326
|
+
|
327
|
+
return selectedDate
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
}
|
346
332
|
|
347
333
|
|
348
334
|
|
14
こードの追加と変更します
test
CHANGED
@@ -118,6 +118,66 @@
|
|
118
118
|
|
119
119
|
DateManager.siwft
|
120
120
|
|
121
|
+
|
122
|
+
|
123
|
+
extension Date {
|
124
|
+
|
125
|
+
// ????修正前 -> NSDate
|
126
|
+
|
127
|
+
func monthAgoDate() -> Date {
|
128
|
+
|
129
|
+
let addValue = -1
|
130
|
+
|
131
|
+
// ????修正前 = NSCalendar.currentCalendar()
|
132
|
+
|
133
|
+
let calendar = Calendar.current
|
134
|
+
|
135
|
+
// ????修正前 let dateComponents = DateComponents()
|
136
|
+
|
137
|
+
var dateComponents = DateComponents()
|
138
|
+
|
139
|
+
dateComponents.month = addValue
|
140
|
+
|
141
|
+
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
142
|
+
|
143
|
+
return calendar.date(byAdding: dateComponents, to: self)!
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
// ????修正前 -> NSDate
|
148
|
+
|
149
|
+
func monthLaterDate() -> Date {
|
150
|
+
|
151
|
+
let addValue: Int = 1
|
152
|
+
|
153
|
+
// ????修正前 = NSCalendar.currentCalendar()
|
154
|
+
|
155
|
+
let calendar = Calendar.current
|
156
|
+
|
157
|
+
// ????修正前 let dateComponents = DateComponents()
|
158
|
+
|
159
|
+
var dateComponents = DateComponents()
|
160
|
+
|
161
|
+
dateComponents.month = addValue
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
//修正前return calendar.date(byAdding: dateComponents, to: self as Date, option: NSCalendar.Options(rawValue: 0))!
|
166
|
+
|
167
|
+
//修正前return calendar.date(byAdding: dateComponents, to: self as Date)!
|
168
|
+
|
169
|
+
//参考サイト : http://stackoverflow.com/questions/24741038/creating-a-future-date-in-swift-with-nsdate
|
170
|
+
|
171
|
+
//回答Claus
|
172
|
+
|
173
|
+
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
174
|
+
|
175
|
+
return calendar.date(byAdding: dateComponents, to: self)!
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
121
181
|
|
122
182
|
|
123
183
|
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
13
コードの表示方法を変更しました
test
CHANGED
File without changes
|
12
間違いを修正します
test
CHANGED
@@ -108,6 +108,14 @@
|
|
108
108
|
|
109
109
|
|
110
110
|
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```ここに言語を入力
|
116
|
+
|
117
|
+
|
118
|
+
|
111
119
|
DateManager.siwft
|
112
120
|
|
113
121
|
|
@@ -278,8 +286,6 @@
|
|
278
286
|
|
279
287
|
|
280
288
|
|
281
|
-
```ここに言語を入力
|
282
|
-
|
283
289
|
コード
|
284
290
|
|
285
291
|
```
|
11
さらに修正しました
test
CHANGED
@@ -107,3 +107,179 @@
|
|
107
107
|
```
|
108
108
|
|
109
109
|
|
110
|
+
|
111
|
+
DateManager.siwft
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
//前月の表示
|
120
|
+
|
121
|
+
func prevMonth(date: NSDate) -> NSDate {
|
122
|
+
|
123
|
+
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
124
|
+
|
125
|
+
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
126
|
+
|
127
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
//次月の表示
|
132
|
+
|
133
|
+
func nextMonth(date: NSDate) -> NSDate {
|
134
|
+
|
135
|
+
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
136
|
+
|
137
|
+
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
138
|
+
|
139
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
以下の「return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’」は修正候補が出ますが「let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’ 」では私の場合はEnpty collection literal requirea na explicit typeとエラーになります、なので先頭のletを削除してエラーが消えました
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
class Date: NSObject {
|
152
|
+
|
153
|
+
//省略
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
//前月の表示
|
158
|
+
|
159
|
+
func prevMonth(date: NSDate) -> NSDate {
|
160
|
+
|
161
|
+
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
162
|
+
|
163
|
+
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
164
|
+
|
165
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
//次月の表示
|
170
|
+
|
171
|
+
func nextMonth(date: NSDate) -> NSDate {
|
172
|
+
|
173
|
+
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
174
|
+
|
175
|
+
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
176
|
+
|
177
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
以下は「Swift3 NSDate」で検索して解決しました
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
//月ごとのセルの数を返すメソッド
|
190
|
+
|
191
|
+
func daysAcquisition() -> Int {
|
192
|
+
|
193
|
+
let rangeOfWeeks = NSCalendar.current.range(of: NSCalendar.Unit.weekOfMonth, in: NSCalendar.Unit.month, for: firstDateOfMonth() as Date) //Cannot convert value of type 'NSCalendar.Unit' to expected argument type 'Calendar.Component' ’エラー13’
|
194
|
+
|
195
|
+
let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数
|
196
|
+
|
197
|
+
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
198
|
+
|
199
|
+
return numberOfItems
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
以下は「Swift3 NSCalendar」で検索して解決しました
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//月の初日を取得
|
210
|
+
|
211
|
+
func firstDateOfMonth() -> NSDate {
|
212
|
+
|
213
|
+
let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’
|
214
|
+
|
215
|
+
fromDate: selectedDate)
|
216
|
+
|
217
|
+
components.day = 1
|
218
|
+
|
219
|
+
let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)!
|
220
|
+
|
221
|
+
return firstDateMonth
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
以下は「ios - How to get the current day number in current month and year」で検索して解決しました
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// ⑴表記する日にちの取得
|
232
|
+
|
233
|
+
func dateForCellAtIndexPath(numberOfItems: Int) {
|
234
|
+
|
235
|
+
// ①「月の初日が週の何日目か」を計算する
|
236
|
+
|
237
|
+
let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’
|
238
|
+
|
239
|
+
for i in 0 ..< numberOfItems {
|
240
|
+
|
241
|
+
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
242
|
+
|
243
|
+
let dateComponents = NSDateComponents()
|
244
|
+
|
245
|
+
dateComponents.day = i - (ordinalityOfFirstDay - 1)
|
246
|
+
|
247
|
+
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
248
|
+
|
249
|
+
let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’
|
250
|
+
|
251
|
+
// ④配列に追加
|
252
|
+
|
253
|
+
currentMonthOfDates.append(date)
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
最後に以下は私の環境でエラーになりません
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// ⑵表記の変更
|
266
|
+
|
267
|
+
func conversionDateFormat(indexPath: NSIndexPath) -> String {
|
268
|
+
|
269
|
+
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
270
|
+
|
271
|
+
let formatter: DateFormatter = DateFormatter()
|
272
|
+
|
273
|
+
formatter.dateFormat = "d"
|
274
|
+
|
275
|
+
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```ここに言語を入力
|
282
|
+
|
283
|
+
コード
|
284
|
+
|
285
|
+
```
|
10
コードを分けて見やすくしました
test
CHANGED
@@ -13,6 +13,8 @@
|
|
13
13
|
|
14
14
|
|
15
15
|
```ここに言語を入力
|
16
|
+
|
17
|
+
|
16
18
|
|
17
19
|
ViewController.siwft
|
18
20
|
|
@@ -98,194 +100,10 @@
|
|
98
100
|
|
99
101
|
}
|
100
102
|
|
101
|
-
|
102
|
-
|
103
|
-
//セルのサイズを設定 あなたのコードです
|
104
|
-
|
105
|
-
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
|
106
|
-
|
107
|
-
let numberOfMargin: CGFloat = 8.0
|
108
|
-
|
109
|
-
let width: CGFloat = (collectionView.frame.size.width - cellMargin * numberOfMargin) / CGFloat(daysPerWeek)
|
110
|
-
|
111
|
-
let height: CGFloat = width * 1.0
|
112
|
-
|
113
|
-
return CGSizeMake(width, height) // 'CGSizeMake' is unavailable in Swift 'エラー4'
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
DateManager.siwft
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
//前月の表示
|
126
|
-
|
127
|
-
func prevMonth(date: NSDate) -> NSDate {
|
128
|
-
|
129
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
130
|
-
|
131
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
132
|
-
|
133
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
//次月の表示
|
138
|
-
|
139
|
-
func nextMonth(date: NSDate) -> NSDate {
|
140
|
-
|
141
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
142
|
-
|
143
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
144
|
-
|
145
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
以下の「return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’」は修正候補が出ますが「let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’ 」では私の場合はEnpty collection literal requirea na explicit typeとエラーになります、なので先頭のletを削除してエラーが消えました
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
class Date: NSObject {
|
158
|
-
|
159
|
-
//省略
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
//前月の表示
|
164
|
-
|
165
|
-
func prevMonth(date: NSDate) -> NSDate {
|
166
|
-
|
167
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
168
|
-
|
169
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
170
|
-
|
171
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
//次月の表示
|
176
|
-
|
177
|
-
func nextMonth(date: NSDate) -> NSDate {
|
178
|
-
|
179
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
180
|
-
|
181
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
182
|
-
|
183
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
以下は「Swift3 NSDate」で検索して解決しました
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
//月ごとのセルの数を返すメソッド
|
196
|
-
|
197
|
-
func daysAcquisition() -> Int {
|
198
|
-
|
199
|
-
let rangeOfWeeks = NSCalendar.current.range(of: NSCalendar.Unit.weekOfMonth, in: NSCalendar.Unit.month, for: firstDateOfMonth() as Date) //Cannot convert value of type 'NSCalendar.Unit' to expected argument type 'Calendar.Component' ’エラー13’
|
200
|
-
|
201
|
-
let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数
|
202
|
-
|
203
|
-
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
204
|
-
|
205
|
-
return numberOfItems
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
以下は「Swift3 NSCalendar」で検索して解決しました
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
//月の初日を取得
|
216
|
-
|
217
|
-
func firstDateOfMonth() -> NSDate {
|
218
|
-
|
219
|
-
let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’
|
220
|
-
|
221
|
-
fromDate: selectedDate)
|
222
|
-
|
223
|
-
components.day = 1
|
224
|
-
|
225
|
-
let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)!
|
226
|
-
|
227
|
-
return firstDateMonth
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
以下は「ios - How to get the current day number in current month and year」で検索して解決しました
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
// ⑴表記する日にちの取得
|
238
|
-
|
239
|
-
func dateForCellAtIndexPath(numberOfItems: Int) {
|
240
|
-
|
241
|
-
// ①「月の初日が週の何日目か」を計算する
|
242
|
-
|
243
|
-
let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’
|
244
|
-
|
245
|
-
for i in 0 ..< numberOfItems {
|
246
|
-
|
247
|
-
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
248
|
-
|
249
|
-
let dateComponents = NSDateComponents()
|
250
|
-
|
251
|
-
dateComponents.day = i - (ordinalityOfFirstDay - 1)
|
252
|
-
|
253
|
-
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
254
|
-
|
255
|
-
let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’
|
256
|
-
|
257
|
-
// ④配列に追加
|
258
|
-
|
259
|
-
currentMonthOfDates.append(date)
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
最後に以下は私の環境でエラーになりません
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
// ⑵表記の変更
|
272
|
-
|
273
|
-
func conversionDateFormat(indexPath: NSIndexPath) -> String {
|
274
|
-
|
275
|
-
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
276
|
-
|
277
|
-
let formatter: DateFormatter = DateFormatter()
|
278
|
-
|
279
|
-
formatter.dateFormat = "d"
|
280
|
-
|
281
|
-
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
103
|
|
288
104
|
|
289
105
|
コード
|
290
106
|
|
291
107
|
```
|
108
|
+
|
109
|
+
|
9
コードを見やすくしました
test
CHANGED
@@ -24,13 +24,13 @@
|
|
24
24
|
|
25
25
|
func changeHeaderTitle() -> String {
|
26
26
|
|
27
|
-
// ????修正前 NSDateFormatter = NSDateFormatter()
|
28
|
-
|
29
|
-
let formatter = DateFormatter()
|
27
|
+
let formatter: DateFormatter = DateFormatter()
|
28
|
+
|
29
|
+
//let formatter = DateFormatter() // ????これでもOKです
|
30
30
|
|
31
31
|
formatter.dateFormat = "M/yyyy"
|
32
32
|
|
33
|
-
// ????修正前 formatter.string
|
33
|
+
// ????修正前 formatter.string(from: date as Date)
|
34
34
|
|
35
35
|
let selectMonth = formatter.string(from: selectedDate)
|
36
36
|
|
@@ -42,15 +42,17 @@
|
|
42
42
|
|
43
43
|
//①タップ時
|
44
44
|
|
45
|
+
// ????修正前 (sender: UIButton)
|
46
|
+
|
45
47
|
@IBAction func tappedHeaderPrevBtn(_ sender: UIButton) {
|
46
48
|
|
49
|
+
// ????修正前 (selectedDate)
|
50
|
+
|
47
|
-
|
51
|
+
selectedDate = dateManager.prevMonth(date: selectedDate)
|
48
|
-
|
49
|
-
dateManager.prevMonth()
|
50
52
|
|
51
53
|
calenderCollectionView.reloadData()
|
52
54
|
|
53
|
-
// ????修正前
|
55
|
+
// ????修正前 (date: selectedDate)
|
54
56
|
|
55
57
|
headerTitle.text = changeHeaderTitle()
|
56
58
|
|
@@ -58,15 +60,17 @@
|
|
58
60
|
|
59
61
|
//②タップ時
|
60
62
|
|
63
|
+
// ????修正前 (sender: UIButton)
|
64
|
+
|
61
65
|
@IBAction func tappedHeaderNextBtn(_ sender: UIButton) {
|
62
66
|
|
67
|
+
// ????修正前 (selectedDate)
|
68
|
+
|
63
|
-
|
69
|
+
selectedDate = dateManager.nextMonth(date: selectedDate)
|
64
|
-
|
65
|
-
dateManager.nextMonth()
|
66
70
|
|
67
71
|
calenderCollectionView.reloadData()
|
68
72
|
|
69
|
-
// ????修正前
|
73
|
+
// ????修正前 (date: selectedDate)
|
70
74
|
|
71
75
|
headerTitle.text = changeHeaderTitle()
|
72
76
|
|
8
間違いを修正するため
test
CHANGED
@@ -30,10 +30,6 @@
|
|
30
30
|
|
31
31
|
formatter.dateFormat = "M/yyyy"
|
32
32
|
|
33
|
-
// ????修正前 下記を追加しました
|
34
|
-
|
35
|
-
let selectedDate = Date()
|
36
|
-
|
37
33
|
// ????修正前 formatter.stringFromDate(date)
|
38
34
|
|
39
35
|
let selectMonth = formatter.string(from: selectedDate)
|
7
段落を修正しました
test
CHANGED
@@ -12,14 +12,12 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
```ここに言語を入力
|
16
16
|
|
17
17
|
ViewController.siwft
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
```ここに言語を入力
|
22
|
-
|
23
21
|
//headerの月を変更
|
24
22
|
|
25
23
|
// ????修正前 (date: NSDate)
|
@@ -291,5 +289,3 @@
|
|
291
289
|
コード
|
292
290
|
|
293
291
|
```
|
294
|
-
|
295
|
-
|
6
コードは ``` で囲うと見やすくなりますよ、の指摘で修正します
test
CHANGED
@@ -18,6 +18,8 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
```ここに言語を入力
|
22
|
+
|
21
23
|
//headerの月を変更
|
22
24
|
|
23
25
|
// ????修正前 (date: NSDate)
|
@@ -281,3 +283,13 @@
|
|
281
283
|
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
282
284
|
|
283
285
|
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
コード
|
292
|
+
|
293
|
+
```
|
294
|
+
|
295
|
+
|
5
文章の更新
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動し
|
1
|
+
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動して落ちなくなりました、ご質問のエラー箇所の解説をいたします、参考にしてください
|
2
2
|
|
3
3
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
4
4
|
|
4
コードの表示方法を変更しました
test
CHANGED
@@ -16,21 +16,73 @@
|
|
16
16
|
|
17
17
|
ViewController.siwft
|
18
18
|
|
19
|
+
|
20
|
+
|
19
|
-
|
21
|
+
//headerの月を変更
|
22
|
+
|
20
|
-
|
23
|
+
// ????修正前 (date: NSDate)
|
24
|
+
|
25
|
+
func changeHeaderTitle() -> String {
|
26
|
+
|
21
|
-
|
27
|
+
// ????修正前 NSDateFormatter = NSDateFormatter()
|
28
|
+
|
22
|
-
|
29
|
+
let formatter = DateFormatter()
|
30
|
+
|
31
|
+
formatter.dateFormat = "M/yyyy"
|
32
|
+
|
33
|
+
// ????修正前 下記を追加しました
|
34
|
+
|
35
|
+
let selectedDate = Date()
|
36
|
+
|
37
|
+
// ????修正前 formatter.stringFromDate(date)
|
38
|
+
|
39
|
+
let selectMonth = formatter.string(from: selectedDate)
|
40
|
+
|
41
|
+
return selectMonth
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
//①タップ時
|
48
|
+
|
49
|
+
@IBAction func tappedHeaderPrevBtn(_ sender: UIButton) {
|
50
|
+
|
23
|
-
//
|
51
|
+
// ????修正前 selectedDate = dateManager.prevMonth(selectedDate)
|
52
|
+
|
24
|
-
|
53
|
+
dateManager.prevMonth()
|
54
|
+
|
55
|
+
calenderCollectionView.reloadData()
|
56
|
+
|
57
|
+
// ????修正前 changeHeaderTitle(selectedDate)
|
58
|
+
|
59
|
+
headerTitle.text = changeHeaderTitle()
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
//②タップ時
|
64
|
+
|
65
|
+
@IBAction func tappedHeaderNextBtn(_ sender: UIButton) {
|
66
|
+
|
25
|
-
//
|
67
|
+
// ????修正前 selectedDate = dateManager.nextMonth(selectedDate)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
68
|
+
|
31
|
-
|
69
|
+
dateManager.nextMonth()
|
70
|
+
|
32
|
-
|
71
|
+
calenderCollectionView.reloadData()
|
72
|
+
|
73
|
+
// ????修正前 changeHeaderTitle(selectedDate)
|
74
|
+
|
75
|
+
headerTitle.text = changeHeaderTitle()
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
33
|
-
|
81
|
+
//セルのサイズを設定
|
82
|
+
|
83
|
+
// ????修正前 (collectionView:
|
84
|
+
|
85
|
+
// ????修正前 sizeForItemAtIndexPath indexPath: NSIndexPath)
|
34
86
|
|
35
87
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
36
88
|
|
@@ -40,9 +92,11 @@
|
|
40
92
|
|
41
93
|
let height: CGFloat = width * 1.0
|
42
94
|
|
95
|
+
// ????修正前 CGSizeMake(width, height)
|
96
|
+
|
43
97
|
return CGSize(width: width, height: height)
|
44
98
|
|
45
|
-
}
|
99
|
+
}
|
46
100
|
|
47
101
|
|
48
102
|
|
3
バージョン追加
test
CHANGED
@@ -4,6 +4,12 @@
|
|
4
4
|
|
5
5
|
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
|
6
6
|
|
7
|
+
Xcode 8.2.1
|
8
|
+
|
9
|
+
Swift 3.0
|
10
|
+
|
11
|
+
macOS Sierra
|
12
|
+
|
7
13
|
|
8
14
|
|
9
15
|
|
2
文章の修正
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
4
4
|
|
5
|
-
解決するまで諦めないでください、もしも、どうしても解決しない場合は私にメー
|
5
|
+
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
|
6
6
|
|
7
7
|
|
8
8
|
|
1
文章の間違え
test
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
|
5
5
|
解決するまで諦めないでください、もしも、どうしても解決しない場合は私にメールしてください
|
6
6
|
|
7
|
-
papassan@hotmail.com
|
8
|
-
|
9
7
|
|
10
8
|
|
11
9
|
|