質問編集履歴

5

コードを追加しました

2020/05/25 05:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -330,6 +330,104 @@
330
330
 
331
331
 
332
332
 
333
+ ### 同じエラーが出る簡略化したコード
334
+
335
+ ```Swift
336
+
337
+ import Cocoa
338
+
339
+ import EventKit
340
+
341
+
342
+
343
+ class ViewController: NSViewController {
344
+
345
+
346
+
347
+ let eventStore:EKEventStore = EKEventStore()
348
+
349
+
350
+
351
+ override func viewDidLoad() {
352
+
353
+ super.viewDidLoad()
354
+
355
+
356
+
357
+ let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
358
+
359
+ if status == .authorized {
360
+
361
+ print("アクセスできます!!")
362
+
363
+ }else if status == .notDetermined {
364
+
365
+ eventStore.requestAccess(to: EKEntityType.event) { (granted, error) in
366
+
367
+ if granted {
368
+
369
+ print("アクセス可能になりました。")
370
+
371
+ }else {
372
+
373
+ print("アクセスが拒否されました。")
374
+
375
+ }
376
+
377
+ }
378
+
379
+ }
380
+
381
+ saveEvent()
382
+
383
+ }
384
+
385
+
386
+
387
+ func saveEvent() {
388
+
389
+ if EKEventStore.authorizationStatus(for: .event) == .authorized{
390
+
391
+
392
+
393
+ let event = EKEvent(eventStore: eventStore)
394
+
395
+ event.title = "sample"
396
+
397
+ event.startDate = Date()
398
+
399
+ event.endDate = Date().addingTimeInterval(2 * 60 * 60)
400
+
401
+ event.isAllDay = false
402
+
403
+ event.calendar = self.eventStore.defaultCalendarForNewEvents
404
+
405
+ do {
406
+
407
+ try eventStore.save(event, span: .thisEvent)
408
+
409
+ }
410
+
411
+ catch {
412
+
413
+ print("Save is failed.")
414
+
415
+ }
416
+
417
+ }
418
+
419
+ }
420
+
421
+
422
+
423
+ }
424
+
425
+
426
+
427
+ ```
428
+
429
+
430
+
333
431
  ### 試したこと
334
432
 
335
433
 

4

コードを書き足しました

2020/05/25 05:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -102,7 +102,33 @@
102
102
 
103
103
  let eventStore:EKEventStore = EKEventStore()
104
104
 
105
-
105
+ var buttons: [NSButton]!
106
+
107
+
108
+
109
+ @IBOutlet weak var startPicker: NSDatePicker!
110
+
111
+ @IBOutlet weak var titleField: NSTextField!
112
+
113
+ @IBOutlet weak var endPicker: NSDatePicker!
114
+
115
+ @IBOutlet weak var datePicker: NSDatePicker!
116
+
117
+ @IBOutlet weak var nonRepeat: NSButton!
118
+
119
+ @IBOutlet weak var everyDay: NSButton!
120
+
121
+ @IBOutlet weak var everyWeek: NSButton!
122
+
123
+ @IBOutlet weak var everyMonth: NSButton!
124
+
125
+ @IBOutlet weak var allDay: NSButton!
126
+
127
+
128
+
129
+ @IBAction func radioButton(_ sender: NSButton) {}
130
+
131
+
106
132
 
107
133
  @IBAction func addButton(_ sender: NSButton) {
108
134
 
@@ -110,6 +136,66 @@
110
136
 
111
137
  }
112
138
 
139
+
140
+
141
+ override func viewDidLoad() {
142
+
143
+ super.viewDidLoad()
144
+
145
+ // Do view setup here.
146
+
147
+ if EKEventStore.authorizationStatus(for: .event) == .notDetermined{
148
+
149
+ eventStore.requestAccess(to: .event, completion: { (granted, error) in
150
+
151
+ if granted && error == nil {
152
+
153
+ print("granted")
154
+
155
+ }
156
+
157
+ else{
158
+
159
+ print("not granted")
160
+
161
+ }
162
+
163
+ })
164
+
165
+ }
166
+
167
+ buttons = [nonRepeat,everyDay,everyWeek,everyMonth]
168
+
169
+ datePicker.locale = Locale.current
170
+
171
+ datePicker.dateValue = Date()
172
+
173
+
174
+
175
+ var component = NSCalendar.current.dateComponents([.year, .month, .day, .hour], from: Date())
176
+
177
+ component.hour = component.hour! + 1
178
+
179
+ component.minute = 0
180
+
181
+ component.second = 0
182
+
183
+ let startDate = NSCalendar.current.date(from:component)! as Date
184
+
185
+ component.hour = component.hour! + 1
186
+
187
+ let endDate = NSCalendar.current.date(from:component)! as Date
188
+
189
+
190
+
191
+ startPicker.dateValue = startDate
192
+
193
+ endPicker.dateValue = endDate
194
+
195
+ }
196
+
197
+
198
+
113
199
  func saveEvent() {
114
200
 
115
201
  if EKEventStore.authorizationStatus(for: .event) == .authorized{
@@ -118,11 +204,11 @@
118
204
 
119
205
  event.title = titleField.stringValue
120
206
 
121
- var dateC = NSCalendar.current.dateComponents([.year, .month, .day], from: startPicker.dateValue)
207
+ var dateC = NSCalendar.current.dateComponents([.year, .month, .day], from: datePicker.dateValue)
122
-
208
+
123
- let startC = NSCalendar.current.dateComponents([.year, .month, .day], from: startPicker.dateValue)
209
+ let startC = NSCalendar.current.dateComponents([.hour, .minute], from: startPicker.dateValue)
124
-
210
+
125
- let endC = NSCalendar.current.dateComponents([.year, .month, .day], from: endPicker.dateValue)
211
+ let endC = NSCalendar.current.dateComponents([.hour, .minute], from: endPicker.dateValue)
126
212
 
127
213
  dateC.second = 0
128
214
 
@@ -130,13 +216,13 @@
130
216
 
131
217
  dateC.minute = startC.minute
132
218
 
133
- let startDate:Date = NSCalendar.current.date(from:dateC)! as Date
219
+ let startDate = NSCalendar.current.date(from:dateC)! as Date
134
220
 
135
221
  dateC.hour = endC.hour
136
222
 
137
223
  dateC.minute = endC.minute
138
224
 
139
- let endDate:Date = NSCalendar.current.date(from:dateC)! as Date
225
+ let endDate = NSCalendar.current.date(from:dateC)! as Date
140
226
 
141
227
 
142
228
 

3

コードを書き足しました

2020/05/24 23:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -90,6 +90,12 @@
90
90
 
91
91
  ```Swift
92
92
 
93
+ import Cocoa
94
+
95
+ import EventKit
96
+
97
+
98
+
93
99
  class AddViewController: NSViewController {
94
100
 
95
101
 

2

カッコが欠けていた

2020/05/24 23:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -232,6 +232,8 @@
232
232
 
233
233
  }
234
234
 
235
+ }
236
+
235
237
  ```
236
238
 
237
239
 

1

コードを追加しました

2020/05/24 23:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,21 @@
90
90
 
91
91
  ```Swift
92
92
 
93
+ class AddViewController: NSViewController {
94
+
95
+
96
+
97
+ let eventStore:EKEventStore = EKEventStore()
98
+
99
+
100
+
101
+ @IBAction func addButton(_ sender: NSButton) {
102
+
103
+ saveEvent()
104
+
105
+ }
106
+
93
- func saveEvent() {
107
+ func saveEvent() {
94
108
 
95
109
  if EKEventStore.authorizationStatus(for: .event) == .authorized{
96
110