質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

6427閲覧

Swift3でのカレンダー実装について

退会済みユーザー

退会済みユーザー

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2017/01/14 05:12

###前提・実現したいこと
iOSにてカレンダーのアプリを作るため、以下の記事を参考に製作しています。
http://qiita.com/sakuran/items/3c2c9f22cbcbf4aff731

###発生している問題・エラーメッセージ
現在swift3で作っていますが、記事がswift3以前のモノだったのでエラーが発生し、解決できていない状況です。

###該当のソースコード
ソース途中のコメント部がエラーメッセージです

swift

1ViewController.siwft 2 //headerの月を変更 3 func changeHeaderTitle(date: NSDate) -> String { 4 let formatter: DateFormatter = DateFormatter() 5 formatter.dateFormat = "M/yyyy" 6 let selectMonth = formatter.string(from: date as Date) //Cannot convert value of type 'NSDate' to type 'Date' in coercion 'エラー1' 7 return selectMonth 8 } 9 10 //①タップ時 11 @IBAction func tappedHeaderPrevBtn(sender: UIButton) { 12 selectedDate = dateManager.prevMonth(selectedDate) 13 calenderCollectionView.reloadData() //Value of type 'DataManager' has no member 'prevMonth' 'エラー2' 14 headerTitle.text = changeHeaderTitle(date: selectedDate) 15 } 16 17 //②タップ時 18 @IBAction func tappedHeaderNextBtn(sender: UIButton) { 19 selectedDate = dateManager.nextMonth(selectedDate) 20 calenderCollectionView.reloadData() //Value of type 'DataManager' has no member 'nextMonth' 'エラー3' 21 headerTitle.text = changeHeaderTitle(date: selectedDate) 22 } 23 24 //セルのサイズを設定 25 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 26 let numberOfMargin: CGFloat = 8.0 27 let width: CGFloat = (collectionView.frame.size.width - cellMargin * numberOfMargin) / CGFloat(daysPerWeek) 28 let height: CGFloat = width * 1.0 29 return CGSizeMake(width, height) // 'CGSizeMake' is unavailable in Swift 'エラー4'
DateManager.siwft import UIKit extension NSDate { func monthAgoDate() -> NSDate { let addValue = -1 let calendar = NSCalendar.current let dateComponents = NSDateComponents() dateComponents.month = addValue return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendar.Options(rawValue: 0))! // Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー5’ } func monthLaterDate() -> NSDate { let addValue: Int = 1 let calendar = NSCalendar.current let dateComponents = NSDateComponents() dateComponents.month = addValue return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendar.Options(rawValue: 0))!// Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー6’ } } class Date: NSObject { //省略 //前月の表示 func prevMonth(date: NSDate) -> NSDate { let currentMonthOfDates = [] // Empty collection literal requires an explicit type ’エラー7’ selectedDate = date.monthAgoDate() //User of unresolved identifier 'selectedDate' ’エラー8’ return selectedDate //User of unresolved identifier 'selectedDate' ’エラー9’ } //次月の表示 func nextMonth(date: NSDate) -> NSDate { currentMonthOfDates = [] //User of unresolved identifier 'currentMonthOfDates' ’エラー10’ selectedDate = date.monthLaterDate() //User of unresolved identifier 'selectedDate' ’エラー11’ return selectedDate //User of unresolved identifier 'selectedDate' ’エラー12’ } } class DateManager: NSObject { var currentMonthOfDates = [NSDate]() //表記する月の配列 var selectedDate = NSDate() let daysPerWeek: Int = 7 var numberOfItems: Int! //月ごとのセルの数を返すメソッド func daysAcquisition() -> Int { 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’ let numberOfWeeks = rangeOfWeeks.length //月が持つ週の数 numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数 return numberOfItems } //月の初日を取得 func firstDateOfMonth() -> NSDate { let components = NSCalendar.currentCalendar.components([.Year, .Month, .Day], //Type of expression is ambiguous without more context ’エラー14’ fromDate: selectedDate) components.day = 1 let firstDateMonth = NSCalendar.currentCalendar().dateFromComponents(components)! return firstDateMonth } // ⑴表記する日にちの取得 func dateForCellAtIndexPath(numberOfItems: Int) { // ①「月の初日が週の何日目か」を計算する let ordinalityOfFirstDay = NSCalendar.currentCalendar.ordinalityOfUnit(NSCalendar.Unit.Day, inUnit: NSCalendar.Unit.WeekOfMonth, forDate: firstDateOfMonth()) // Value of type 'Calendar' has no member 'ordinalityOfUnit' ’エラー15’ for i in 0 ..< numberOfItems { // ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する let dateComponents = NSDateComponents() dateComponents.day = i - (ordinalityOfFirstDay - 1) // ③ 表示する月の初日から②で計算した差を引いた日付を取得 let date = NSCalendar.currentCalendar.dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendar.Options(rawValue: 0))! //Value of type 'Calendar' has no member 'dateByAddingComponents' ’エラー16’ // ④配列に追加 currentMonthOfDates.append(date) } } // ⑵表記の変更 func conversionDateFormat(indexPath: NSIndexPath) -> String { dateForCellAtIndexPath(numberOfItems: numberOfItems) let formatter: DateFormatter = DateFormatter() formatter.dateFormat = "d" return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) // Cannot convert value of type 'NSDate' to type 'Date' in coercion ’エラー17’ } }

###試したこと
色々調べて試しましたが解決できないため質問させていただきました。
よろしくお願いいたします。

###補足情報(言語/FW/ツール等のバージョンなど)
足りない情報等あれば追記致します。
エラーが多いですが、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

私も同じカレンダーを参考にしてSwift3用にエラー修正をしていますが私の場合はエラーは修正して起動して落ちなくなりました、ご質問のエラー箇所の解説をいたします、参考にしてください
早速ですが、私の意見で解説しますが、それを参考にしてもう少し努力してください、必ず解決しますよ
解決するまで諦めないでください、もしも、どうしても解決しない場合は私のFacebookにメッセージしてください
Xcode 8.2.1
Swift 3.0
macOS Sierra

ViewController.siwft //headerの月を変更 // ????修正前 (date: NSDate) func changeHeaderTitle() -> String { let formatter: DateFormatter = DateFormatter() //let formatter = DateFormatter() // ????これでもOKです formatter.dateFormat = "M/yyyy" // ????修正前 formatter.string(from: date as Date) let selectMonth = formatter.string(from: selectedDate) return selectMonth } //①タップ時 // ????修正前 (sender: UIButton) @IBAction func tappedHeaderPrevBtn(_ sender: UIButton) { // ????修正前 (selectedDate) selectedDate = dateManager.prevMonth(date: selectedDate) calenderCollectionView.reloadData() // ????修正前 (date: selectedDate) headerTitle.text = changeHeaderTitle() } //②タップ時 // ????修正前 (sender: UIButton) @IBAction func tappedHeaderNextBtn(_ sender: UIButton) { // ????修正前 (selectedDate) selectedDate = dateManager.nextMonth(date: selectedDate) calenderCollectionView.reloadData() // ????修正前 (date: selectedDate) headerTitle.text = changeHeaderTitle() } //セルのサイズを設定 // ????修正前 (collectionView: // ????修正前 sizeForItemAtIndexPath indexPath: NSIndexPath) func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let numberOfMargin: CGFloat = 8.0 let width: CGFloat = (collectionView.frame.size.width - cellMargin * numberOfMargin) / CGFloat(daysPerWeek) let height: CGFloat = width * 1.0 // ????修正前 CGSizeMake(width, height) return CGSize(width: width, height: height) } コード
DateManager.siwft import UIKit extension Date { // ????修正前 -> NSDate func monthAgoDate() -> Date { let addValue = -1 // ????修正前 = NSCalendar.currentCalendar() let calendar = Calendar.current // ????修正前 let dateComponents = DateComponents() var dateComponents = DateComponents() dateComponents.month = addValue // ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))! return calendar.date(byAdding: dateComponents, to: self)! } // ????修正前 -> NSDate func monthLaterDate() -> Date { let addValue: Int = 1 // ????修正前 = NSCalendar.currentCalendar() let calendar = Calendar.current // ????修正前 let dateComponents = DateComponents() var dateComponents = DateComponents() dateComponents.month = addValue // ????修正前 return calendar.dateByAddingComponents(dateComponents, toDate: self, options: NSCalendarOptions(rawValue: 0))! return calendar.date(byAdding: dateComponents, to: self)! } } class DateManager: NSObject { var currentMonthOfDates = [NSDate]() //表記する月の配列 // ????修正前 NSDate() var selectedDate = Date() let daysPerWeek: Int = 7 // ????修正前 Int! var numberOfItems: Int! = 0 //セルの個数 nilが入らないようにする //月ごとのセルの数を返すメソッド func daysAcquisition() -> Int { // ????修正前 NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfMonth, inUnit: NSCalendarUnit.Month, forDate: firstDateOfMonth()) let rangeOfWeeks = Calendar.current.range(of: .weekOfMonth, in: .month, for: firstDateOfMonth() as Date) // ????修正前 rangeOfWeeks.length let numberOfWeeks = Int((rangeOfWeeks?.count)!) //月が持つ週の数 numberOfItems = numberOfWeeks * daysPerWeek //週の数×列の数 return numberOfItems } //月の初日を取得 func firstDateOfMonth() -> Date { // ????修正前 let components = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: selectedDate) var components = Calendar.current.dateComponents([.year, .month, .day], from:selectedDate) components.day = 1 // ????修正前 NSCalendar.currentCalendar().dateFromComponents(components)! let firstDateMonth = Calendar.current.date(from: components)! return firstDateMonth } // ⑴表記する日にちの取得 func dateForCellAtIndexPath(numberOfItem: Int) { // ①「月の初日が週の何日目か」を計算する // ????修正前 NSCalendar.currentCalendar().ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.WeekOfMonth, forDate: firstDateOfMonth()) let ordinalityOfFirstDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: firstDateOfMonth()) // ????修正前 for var i = 0; i < numberOfItems; i++ { for i in 0 ..< numberOfItems { // ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する var dateComponents = DateComponents() // ????修正前 (ordinalityOfFirstDay - 1) dateComponents.day = i - (ordinalityOfFirstDay! - 1) // ③ 表示する月の初日から②で計算した差を引いた日付を取得 // ????修正前 NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: firstDateOfMonth(), options: NSCalendarOptions(rawValue: 0))! let date = Calendar.current.date(byAdding: dateComponents as DateComponents, to: firstDateOfMonth() as Date)! // ④配列に追加 // ????修正前 (date) currentMonthOfDates.append(date as NSDate) } } // ⑵表記の変更 // ????修正前 (indexPath: NSIndexPath) func conversionDateFormat(indexPath: IndexPath) -> String { // ????修正前 (numberOfItems) dateForCellAtIndexPath(numberOfItem: numberOfItems) // ????修正前 NSDateFormatter = NSDateFormatter() let formatter: DateFormatter = DateFormatter() formatter.dateFormat = "d" // ????修正前 return formatter.stringFromDate(currentMonthOfDates[indexPath.row]) return formatter.string(from: currentMonthOfDates[indexPath.row] as Date) } //前月の表示 // ????修正前 (date: NSDate) -> NSDate func prevMonth(date: Date) -> Date { currentMonthOfDates = [] selectedDate = date.monthAgoDate() return selectedDate } //次月の表示 // ????修正前 (date: NSDate) -> NSDate func nextMonth(date: Date) -> Date { currentMonthOfDates = [] selectedDate = date.monthLaterDate() return selectedDate } } コード

投稿2017/01/15 01:22

編集2017/01/18 05:31
papassan

総合スコア36

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

papassan

2017/01/17 07:02

知りませんでした、ご指摘ありがとうございました、感謝いたします
退会済みユーザー

退会済みユーザー

2017/01/18 16:16

記載していただいた通りで解決できました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問