回答編集履歴
15
間違えを修正します
answer
CHANGED
@@ -59,6 +59,8 @@
|
|
59
59
|
|
60
60
|
DateManager.siwft
|
61
61
|
|
62
|
+
import UIKit
|
63
|
+
|
62
64
|
extension Date {
|
63
65
|
// ????修正前 -> NSDate
|
64
66
|
func monthAgoDate() -> Date {
|
@@ -80,97 +82,88 @@
|
|
80
82
|
var dateComponents = DateComponents()
|
81
83
|
dateComponents.month = addValue
|
82
84
|
|
83
|
-
//修正前return calendar.date(byAdding: dateComponents, to: self as Date, option: NSCalendar.Options(rawValue: 0))!
|
84
|
-
//修正前return calendar.date(byAdding: dateComponents, to: self as Date)!
|
85
|
-
//参考サイト : http://stackoverflow.com/questions/24741038/creating-a-future-date-in-swift-with-nsdate
|
86
|
-
//回答Claus
|
87
85
|
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
88
86
|
return calendar.date(byAdding: dateComponents, to: self)!
|
89
87
|
}
|
90
88
|
}
|
91
|
-
|
92
|
-
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
93
|
-
|
94
|
-
//前月の表示
|
95
|
-
func prevMonth(date: NSDate) -> NSDate {
|
96
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
97
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
98
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
99
|
-
}
|
100
|
-
//次月の表示
|
101
|
-
func nextMonth(date: NSDate) -> NSDate {
|
102
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
103
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
104
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
以下の「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を削除してエラーが消えました
|
109
|
-
|
110
|
-
class Date: NSObject {
|
111
|
-
//省略
|
112
|
-
|
113
|
-
//前月の表示
|
114
|
-
func prevMonth(date: NSDate) -> NSDate {
|
115
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
116
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
117
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
118
|
-
}
|
119
|
-
//次月の表示
|
120
|
-
func nextMonth(date: NSDate) -> NSDate {
|
121
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
122
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
123
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
124
|
-
}
|
125
|
-
}
|
126
|
-
|
127
|
-
以下は「Swift3 NSDate」で検索して解決しました
|
128
|
-
|
129
|
-
//月ごとのセルの数を返すメソッド
|
130
|
-
func daysAcquisition() -> Int {
|
131
|
-
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’
|
132
|
-
let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数
|
133
|
-
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
134
|
-
return numberOfItems
|
135
|
-
}
|
136
|
-
|
137
|
-
以下は「Swift3 NSCalendar」で検索して解決しました
|
138
|
-
|
139
|
-
//月の初日を取得
|
140
|
-
func firstDateOfMonth() -> NSDate {
|
141
|
-
let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’
|
142
|
-
fromDate: selectedDate)
|
143
|
-
components.day = 1
|
144
|
-
let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)!
|
145
|
-
return firstDateMonth
|
146
|
-
}
|
147
|
-
|
148
|
-
以下は「ios - How to get the current day number in current month and year」で検索して解決しました
|
149
|
-
|
150
|
-
// ⑴表記する日にちの取得
|
151
|
-
func dateForCellAtIndexPath(numberOfItems: Int) {
|
152
|
-
// ①「月の初日が週の何日目か」を計算する
|
153
|
-
let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’
|
154
|
-
for i in 0 ..< numberOfItems {
|
155
|
-
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
156
|
-
let dateComponents = NSDateComponents()
|
157
|
-
dateComponents.day = i - (ordinalityOfFirstDay - 1)
|
158
|
-
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
159
|
-
let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’
|
160
|
-
// ④配列に追加
|
161
|
-
currentMonthOfDates.append(date)
|
162
|
-
}
|
163
|
-
}
|
164
|
-
|
165
|
-
最後に以下は私の環境でエラーになりません
|
166
|
-
|
167
|
-
// ⑵表記の変更
|
168
|
-
func conversionDateFormat(indexPath: NSIndexPath) -> String {
|
169
|
-
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
170
|
-
let formatter: DateFormatter = DateFormatter()
|
171
|
-
formatter.dateFormat = "d"
|
172
|
-
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
173
|
-
}
|
174
89
|
|
90
|
+
class DateManager: NSObject {
|
91
|
+
|
92
|
+
var currentMonthOfDates = [NSDate]() //表記する月の配列
|
93
|
+
// ????修正前 NSDate()
|
94
|
+
var selectedDate = Date()
|
95
|
+
let daysPerWeek: Int = 7
|
96
|
+
// ????修正前 Int!
|
97
|
+
var numberOfItems: Int! = 0 //セルの個数 nilが入らないようにする
|
98
|
+
|
99
|
+
//月ごとのセルの数を返すメソッド
|
100
|
+
func daysAcquisition() -> Int {
|
101
|
+
// ????修正前 NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfMonth, inUnit: NSCalendarUnit.Month, forDate: firstDateOfMonth())
|
102
|
+
let rangeOfWeeks = Calendar.current.range(of: .weekOfMonth, in: .month, for: firstDateOfMonth() as Date)
|
103
|
+
|
104
|
+
// ????修正前 rangeOfWeeks.length
|
105
|
+
let numberOfWeeks = Int((rangeOfWeeks?.count)!) //月が持つ週の数
|
106
|
+
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
107
|
+
return numberOfItems
|
108
|
+
}
|
109
|
+
//月の初日を取得
|
110
|
+
func firstDateOfMonth() -> Date {
|
111
|
+
// ????修正前 let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: selectedDate)
|
112
|
+
var components = Calendar.current.dateComponents([.year, .month, .day], from:selectedDate)
|
113
|
+
components.day = 1
|
114
|
+
// ????修正前 NSCalendar.currentCalendar().dateFromComponents(components)!
|
115
|
+
let firstDateMonth = Calendar.current.date(from: components)!
|
116
|
+
return firstDateMonth
|
117
|
+
}
|
118
|
+
|
119
|
+
// ⑴表記する日にちの取得
|
120
|
+
func dateForCellAtIndexPath(numberOfItem: Int) {
|
121
|
+
// ①「月の初日が週の何日目か」を計算する
|
122
|
+
// ????修正前 NSCalendar.currentCalendar().ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.WeekOfMonth, forDate: firstDateOfMonth())
|
123
|
+
let ordinalityOfFirstDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: firstDateOfMonth())
|
124
|
+
// ????修正前 for var i = 0; i < numberOfItems; i++ {
|
125
|
+
for i in 0 ..< numberOfItems {
|
126
|
+
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
127
|
+
var dateComponents = DateComponents()
|
128
|
+
// ????修正前 (ordinalityOfFirstDay - 1)
|
129
|
+
dateComponents.day = i - (ordinalityOfFirstDay! - 1)
|
130
|
+
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
131
|
+
// ????修正前 NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendarOptions(rawValue: 0))!
|
132
|
+
let date = Calendar.current.date(byAdding: dateComponents as DateComponents, to: firstDateOfMonth() as Date)!
|
133
|
+
// ④配列に追加
|
134
|
+
// ????修正前 (date)
|
135
|
+
currentMonthOfDates.append(date as NSDate)
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
// ⑵表記の変更
|
140
|
+
// ????修正前 (indexPath: NSIndexPath)
|
141
|
+
func conversionDateFormat(indexPath: IndexPath) -> String {
|
142
|
+
// ????修正前 (numberOfItems)
|
143
|
+
dateForCellAtIndexPath(numberOfItem: numberOfItems)
|
144
|
+
// ????修正前 NSDateFormatter = NSDateFormatter()
|
145
|
+
let formatter: DateFormatter = DateFormatter()
|
146
|
+
formatter.dateFormat = "d"
|
147
|
+
// ????修正前 return formatter.stringFromDate(currentMonthOfDates[indexPath.row])
|
148
|
+
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date)
|
149
|
+
}
|
150
|
+
|
151
|
+
//前月の表示
|
152
|
+
// ????修正前 (date: NSDate) -> NSDate
|
153
|
+
func prevMonth(date: Date) -> Date {
|
154
|
+
currentMonthOfDates = []
|
155
|
+
selectedDate = date.monthAgoDate()
|
156
|
+
return selectedDate
|
157
|
+
}
|
158
|
+
|
159
|
+
//次月の表示
|
160
|
+
// ????修正前 (date: NSDate) -> NSDate
|
161
|
+
func nextMonth(date: Date) -> Date {
|
162
|
+
currentMonthOfDates = []
|
163
|
+
selectedDate = date.monthLaterDate()
|
164
|
+
return selectedDate
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
175
168
|
コード
|
176
169
|
```
|
14
こードの追加と変更します
answer
CHANGED
@@ -58,6 +58,36 @@
|
|
58
58
|
```ここに言語を入力
|
59
59
|
|
60
60
|
DateManager.siwft
|
61
|
+
|
62
|
+
extension Date {
|
63
|
+
// ????修正前 -> NSDate
|
64
|
+
func monthAgoDate() -> Date {
|
65
|
+
let addValue = -1
|
66
|
+
// ????修正前 = NSCalendar.currentCalendar()
|
67
|
+
let calendar = Calendar.current
|
68
|
+
// ????修正前 let dateComponents = DateComponents()
|
69
|
+
var dateComponents = DateComponents()
|
70
|
+
dateComponents.month = addValue
|
71
|
+
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
72
|
+
return calendar.date(byAdding: dateComponents, to: self)!
|
73
|
+
}
|
74
|
+
// ????修正前 -> NSDate
|
75
|
+
func monthLaterDate() -> Date {
|
76
|
+
let addValue: Int = 1
|
77
|
+
// ????修正前 = NSCalendar.currentCalendar()
|
78
|
+
let calendar = Calendar.current
|
79
|
+
// ????修正前 let dateComponents = DateComponents()
|
80
|
+
var dateComponents = DateComponents()
|
81
|
+
dateComponents.month = addValue
|
82
|
+
|
83
|
+
//修正前return calendar.date(byAdding: dateComponents, to: self as Date, option: NSCalendar.Options(rawValue: 0))!
|
84
|
+
//修正前return calendar.date(byAdding: dateComponents, to: self as Date)!
|
85
|
+
//参考サイト : http://stackoverflow.com/questions/24741038/creating-a-future-date-in-swift-with-nsdate
|
86
|
+
//回答Claus
|
87
|
+
// ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))!
|
88
|
+
return calendar.date(byAdding: dateComponents, to: self)!
|
89
|
+
}
|
90
|
+
}
|
61
91
|
|
62
92
|
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
63
93
|
|
13
コードの表示方法を変更しました
answer
CHANGED
File without changes
|
12
間違いを修正します
answer
CHANGED
@@ -53,6 +53,10 @@
|
|
53
53
|
コード
|
54
54
|
```
|
55
55
|
|
56
|
+
|
57
|
+
|
58
|
+
```ここに言語を入力
|
59
|
+
|
56
60
|
DateManager.siwft
|
57
61
|
|
58
62
|
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
@@ -138,6 +142,5 @@
|
|
138
142
|
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
139
143
|
}
|
140
144
|
|
141
|
-
```ここに言語を入力
|
142
145
|
コード
|
143
146
|
```
|
11
さらに修正しました
answer
CHANGED
@@ -52,3 +52,92 @@
|
|
52
52
|
|
53
53
|
コード
|
54
54
|
```
|
55
|
+
|
56
|
+
DateManager.siwft
|
57
|
+
|
58
|
+
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
59
|
+
|
60
|
+
//前月の表示
|
61
|
+
func prevMonth(date: NSDate) -> NSDate {
|
62
|
+
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
63
|
+
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
64
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
65
|
+
}
|
66
|
+
//次月の表示
|
67
|
+
func nextMonth(date: NSDate) -> NSDate {
|
68
|
+
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
69
|
+
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
70
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
以下の「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を削除してエラーが消えました
|
75
|
+
|
76
|
+
class Date: NSObject {
|
77
|
+
//省略
|
78
|
+
|
79
|
+
//前月の表示
|
80
|
+
func prevMonth(date: NSDate) -> NSDate {
|
81
|
+
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
82
|
+
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
83
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
84
|
+
}
|
85
|
+
//次月の表示
|
86
|
+
func nextMonth(date: NSDate) -> NSDate {
|
87
|
+
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
88
|
+
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
89
|
+
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
以下は「Swift3 NSDate」で検索して解決しました
|
94
|
+
|
95
|
+
//月ごとのセルの数を返すメソッド
|
96
|
+
func daysAcquisition() -> Int {
|
97
|
+
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’
|
98
|
+
let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数
|
99
|
+
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
100
|
+
return numberOfItems
|
101
|
+
}
|
102
|
+
|
103
|
+
以下は「Swift3 NSCalendar」で検索して解決しました
|
104
|
+
|
105
|
+
//月の初日を取得
|
106
|
+
func firstDateOfMonth() -> NSDate {
|
107
|
+
let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’
|
108
|
+
fromDate: selectedDate)
|
109
|
+
components.day = 1
|
110
|
+
let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)!
|
111
|
+
return firstDateMonth
|
112
|
+
}
|
113
|
+
|
114
|
+
以下は「ios - How to get the current day number in current month and year」で検索して解決しました
|
115
|
+
|
116
|
+
// ⑴表記する日にちの取得
|
117
|
+
func dateForCellAtIndexPath(numberOfItems: Int) {
|
118
|
+
// ①「月の初日が週の何日目か」を計算する
|
119
|
+
let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’
|
120
|
+
for i in 0 ..< numberOfItems {
|
121
|
+
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
122
|
+
let dateComponents = NSDateComponents()
|
123
|
+
dateComponents.day = i - (ordinalityOfFirstDay - 1)
|
124
|
+
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
125
|
+
let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’
|
126
|
+
// ④配列に追加
|
127
|
+
currentMonthOfDates.append(date)
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
最後に以下は私の環境でエラーになりません
|
132
|
+
|
133
|
+
// ⑵表記の変更
|
134
|
+
func conversionDateFormat(indexPath: NSIndexPath) -> String {
|
135
|
+
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
136
|
+
let formatter: DateFormatter = DateFormatter()
|
137
|
+
formatter.dateFormat = "d"
|
138
|
+
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
139
|
+
}
|
140
|
+
|
141
|
+
```ここに言語を入力
|
142
|
+
コード
|
143
|
+
```
|
10
コードを分けて見やすくしました
answer
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
macOS Sierra
|
7
7
|
|
8
8
|
```ここに言語を入力
|
9
|
+
|
9
10
|
ViewController.siwft
|
10
11
|
|
11
12
|
//headerの月を変更
|
@@ -48,99 +49,6 @@
|
|
48
49
|
// ????修正前 CGSizeMake(width, height)
|
49
50
|
return CGSize(width: width, height: height)
|
50
51
|
}
|
51
|
-
|
52
|
-
//セルのサイズを設定 あなたのコードです
|
53
|
-
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
|
54
|
-
let numberOfMargin: CGFloat = 8.0
|
55
|
-
let width: CGFloat = (collectionView.frame.size.width - cellMargin * numberOfMargin) / CGFloat(daysPerWeek)
|
56
|
-
let height: CGFloat = width * 1.0
|
57
|
-
return CGSizeMake(width, height) // 'CGSizeMake' is unavailable in Swift 'エラー4'
|
58
52
|
|
59
|
-
DateManager.siwft
|
60
|
-
|
61
|
-
以下は「Swift 3 - XCode 8 with Date class instead of NSDate and Calender instead of NSCalender」で検索してください、私はエラーは取れました
|
62
|
-
|
63
|
-
//前月の表示
|
64
|
-
func prevMonth(date: NSDate) -> NSDate {
|
65
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
66
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
67
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
68
|
-
}
|
69
|
-
//次月の表示
|
70
|
-
func nextMonth(date: NSDate) -> NSDate {
|
71
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
72
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
73
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
74
|
-
}
|
75
|
-
}
|
76
|
-
|
77
|
-
以下の「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を削除してエラーが消えました
|
78
|
-
|
79
|
-
class Date: NSObject {
|
80
|
-
//省略
|
81
|
-
|
82
|
-
//前月の表示
|
83
|
-
func prevMonth(date: NSDate) -> NSDate {
|
84
|
-
let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’
|
85
|
-
selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’
|
86
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’
|
87
|
-
}
|
88
|
-
//次月の表示
|
89
|
-
func nextMonth(date: NSDate) -> NSDate {
|
90
|
-
currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’
|
91
|
-
selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’
|
92
|
-
return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
以下は「Swift3 NSDate」で検索して解決しました
|
97
|
-
|
98
|
-
//月ごとのセルの数を返すメソッド
|
99
|
-
func daysAcquisition() -> Int {
|
100
|
-
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’
|
101
|
-
let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数
|
102
|
-
numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数
|
103
|
-
return numberOfItems
|
104
|
-
}
|
105
|
-
|
106
|
-
以下は「Swift3 NSCalendar」で検索して解決しました
|
107
|
-
|
108
|
-
//月の初日を取得
|
109
|
-
func firstDateOfMonth() -> NSDate {
|
110
|
-
let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’
|
111
|
-
fromDate: selectedDate)
|
112
|
-
components.day = 1
|
113
|
-
let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)!
|
114
|
-
return firstDateMonth
|
115
|
-
}
|
116
|
-
|
117
|
-
以下は「ios - How to get the current day number in current month and year」で検索して解決しました
|
118
|
-
|
119
|
-
// ⑴表記する日にちの取得
|
120
|
-
func dateForCellAtIndexPath(numberOfItems: Int) {
|
121
|
-
// ①「月の初日が週の何日目か」を計算する
|
122
|
-
let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’
|
123
|
-
for i in 0 ..< numberOfItems {
|
124
|
-
// ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
|
125
|
-
let dateComponents = NSDateComponents()
|
126
|
-
dateComponents.day = i - (ordinalityOfFirstDay - 1)
|
127
|
-
// ③ 表示する月の初日から②で計算した差を引いた日付を取得
|
128
|
-
let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’
|
129
|
-
// ④配列に追加
|
130
|
-
currentMonthOfDates.append(date)
|
131
|
-
}
|
132
|
-
}
|
133
|
-
|
134
|
-
最後に以下は私の環境でエラーになりません
|
135
|
-
|
136
|
-
// ⑵表記の変更
|
137
|
-
func conversionDateFormat(indexPath: NSIndexPath) -> String {
|
138
|
-
dateForCellAtIndexPath(numberOfItems: numberOfItems)
|
139
|
-
let formatter: DateFormatter = DateFormatter()
|
140
|
-
formatter.dateFormat = "d"
|
141
|
-
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
142
|
-
}
|
143
|
-
|
144
|
-
|
145
53
|
コード
|
146
|
-
```
|
54
|
+
```
|
9
コードを見やすくしました
answer
CHANGED
@@ -11,28 +11,30 @@
|
|
11
11
|
//headerの月を変更
|
12
12
|
// ????修正前 (date: NSDate)
|
13
13
|
func changeHeaderTitle() -> String {
|
14
|
-
// ????修正前 NSDateFormatter = NSDateFormatter()
|
15
|
-
let formatter = DateFormatter()
|
14
|
+
let formatter: DateFormatter = DateFormatter()
|
15
|
+
//let formatter = DateFormatter() // ????これでもOKです
|
16
16
|
formatter.dateFormat = "M/yyyy"
|
17
|
-
// ????修正前 formatter.
|
17
|
+
// ????修正前 formatter.string(from: date as Date)
|
18
18
|
let selectMonth = formatter.string(from: selectedDate)
|
19
19
|
return selectMonth
|
20
20
|
}
|
21
21
|
|
22
22
|
//①タップ時
|
23
|
+
// ????修正前 (sender: UIButton)
|
23
24
|
@IBAction func tappedHeaderPrevBtn(_ sender: UIButton) {
|
25
|
+
// ????修正前 (selectedDate)
|
24
|
-
|
26
|
+
selectedDate = dateManager.prevMonth(date: selectedDate)
|
25
|
-
dateManager.prevMonth()
|
26
27
|
calenderCollectionView.reloadData()
|
27
|
-
// ????修正前
|
28
|
+
// ????修正前 (date: selectedDate)
|
28
29
|
headerTitle.text = changeHeaderTitle()
|
29
30
|
}
|
30
31
|
//②タップ時
|
32
|
+
// ????修正前 (sender: UIButton)
|
31
33
|
@IBAction func tappedHeaderNextBtn(_ sender: UIButton) {
|
34
|
+
// ????修正前 (selectedDate)
|
32
|
-
|
35
|
+
selectedDate = dateManager.nextMonth(date: selectedDate)
|
33
|
-
dateManager.nextMonth()
|
34
36
|
calenderCollectionView.reloadData()
|
35
|
-
// ????修正前
|
37
|
+
// ????修正前 (date: selectedDate)
|
36
38
|
headerTitle.text = changeHeaderTitle()
|
37
39
|
}
|
38
40
|
|
8
間違いを修正するため
answer
CHANGED
@@ -14,8 +14,6 @@
|
|
14
14
|
// ????修正前 NSDateFormatter = NSDateFormatter()
|
15
15
|
let formatter = DateFormatter()
|
16
16
|
formatter.dateFormat = "M/yyyy"
|
17
|
-
// ????修正前 下記を追加しました
|
18
|
-
let selectedDate = Date()
|
19
17
|
// ????修正前 formatter.stringFromDate(date)
|
20
18
|
let selectMonth = formatter.string(from: selectedDate)
|
21
19
|
return selectMonth
|
7
段落を修正しました
answer
CHANGED
@@ -5,10 +5,9 @@
|
|
5
5
|
Swift 3.0
|
6
6
|
macOS Sierra
|
7
7
|
|
8
|
-
|
8
|
+
```ここに言語を入力
|
9
9
|
ViewController.siwft
|
10
10
|
|
11
|
-
```ここに言語を入力
|
12
11
|
//headerの月を変更
|
13
12
|
// ????修正前 (date: NSDate)
|
14
13
|
func changeHeaderTitle() -> String {
|
@@ -144,4 +143,4 @@
|
|
144
143
|
|
145
144
|
|
146
145
|
コード
|
147
|
-
```
|
146
|
+
```
|
6
コードは ``` で囲うと見やすくなりますよ、の指摘で修正します
answer
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
|
9
9
|
ViewController.siwft
|
10
10
|
|
11
|
+
```ここに言語を入力
|
11
12
|
//headerの月を変更
|
12
13
|
// ????修正前 (date: NSDate)
|
13
14
|
func changeHeaderTitle() -> String {
|
@@ -139,4 +140,8 @@
|
|
139
140
|
let formatter: DateFormatter = DateFormatter()
|
140
141
|
formatter.dateFormat = "d"
|
141
142
|
return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’
|
142
|
-
}
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
コード
|
147
|
+
```
|
5
文章の更新
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動し
|
1
|
+
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動して落ちなくなりました、ご質問のエラー箇所の解説をいたします、参考にしてください
|
2
2
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
3
3
|
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
|
4
4
|
Xcode 8.2.1
|
4
コードの表示方法を変更しました
answer
CHANGED
@@ -7,20 +7,47 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
ViewController.siwft
|
10
|
-
私の環境では以下のエラーは出ません
|
11
|
-
//Cannot convert value of type 'NSDate' to type 'Date' in coercion 'エラー1'
|
12
|
-
//Value of type 'DataManager' has no member 'prevMonth' 'エラー2'
|
13
|
-
//Value of type 'DataManager' has no member 'nextMonth' 'エラー3'
|
14
10
|
|
11
|
+
//headerの月を変更
|
12
|
+
// ????修正前 (date: NSDate)
|
13
|
+
func changeHeaderTitle() -> String {
|
14
|
+
// ????修正前 NSDateFormatter = NSDateFormatter()
|
15
|
+
let formatter = DateFormatter()
|
16
|
+
formatter.dateFormat = "M/yyyy"
|
17
|
+
// ????修正前 下記を追加しました
|
18
|
+
let selectedDate = Date()
|
19
|
+
// ????修正前 formatter.stringFromDate(date)
|
20
|
+
let selectMonth = formatter.string(from: selectedDate)
|
21
|
+
return selectMonth
|
22
|
+
}
|
23
|
+
|
24
|
+
//①タップ時
|
25
|
+
@IBAction func tappedHeaderPrevBtn(_ sender: UIButton) {
|
26
|
+
// ????修正前 selectedDate = dateManager.prevMonth(selectedDate)
|
27
|
+
dateManager.prevMonth()
|
28
|
+
calenderCollectionView.reloadData()
|
29
|
+
// ????修正前 changeHeaderTitle(selectedDate)
|
30
|
+
headerTitle.text = changeHeaderTitle()
|
31
|
+
}
|
32
|
+
//②タップ時
|
33
|
+
@IBAction func tappedHeaderNextBtn(_ sender: UIButton) {
|
34
|
+
// ????修正前 selectedDate = dateManager.nextMonth(selectedDate)
|
35
|
+
dateManager.nextMonth()
|
36
|
+
calenderCollectionView.reloadData()
|
37
|
+
// ????修正前 changeHeaderTitle(selectedDate)
|
38
|
+
headerTitle.text = changeHeaderTitle()
|
39
|
+
}
|
15
40
|
|
16
|
-
/*
|
17
|
-
|
41
|
+
//セルのサイズを設定
|
42
|
+
// ????修正前 (collectionView:
|
43
|
+
// ????修正前 sizeForItemAtIndexPath indexPath: NSIndexPath)
|
18
44
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
19
45
|
let numberOfMargin: CGFloat = 8.0
|
20
46
|
let width: CGFloat = (collectionView.frame.size.width - cellMargin * numberOfMargin) / CGFloat(daysPerWeek)
|
21
47
|
let height: CGFloat = width * 1.0
|
48
|
+
// ????修正前 CGSizeMake(width, height)
|
22
49
|
return CGSize(width: width, height: height)
|
23
|
-
}
|
50
|
+
}
|
24
51
|
|
25
52
|
//セルのサイズを設定 あなたのコードです
|
26
53
|
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
|
3
バージョン追加
answer
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動しますが落ちる状態です、でも、ご質問のエラーは修正していますので解説します
|
2
2
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
3
3
|
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
|
4
|
+
Xcode 8.2.1
|
5
|
+
Swift 3.0
|
6
|
+
macOS Sierra
|
4
7
|
|
5
8
|
|
6
9
|
ViewController.siwft
|
2
文章の修正
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動しますが落ちる状態です、でも、ご質問のエラーは修正していますので解説します
|
2
2
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
3
|
-
解決するまで諦めないでください、もしも、どうしても解決しない場合は私にメー
|
3
|
+
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
|
4
4
|
|
5
5
|
|
6
6
|
ViewController.siwft
|
1
文章の間違え
answer
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動しますが落ちる状態です、でも、ご質問のエラーは修正していますので解説します
|
2
2
|
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
|
3
3
|
解決するまで諦めないでください、もしも、どうしても解決しない場合は私にメールしてください
|
4
|
-
papassan@hotmail.com
|
5
4
|
|
6
5
|
|
7
6
|
ViewController.siwft
|