Skip to content

Instantly share code, notes, and snippets.

@tkymx
Created May 7, 2017 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkymx/56ea94aacee0ae7acda55f38485974ee to your computer and use it in GitHub Desktop.
Save tkymx/56ea94aacee0ae7acda55f38485974ee to your computer and use it in GitHub Desktop.
getToDay 何日前かを指定して日付をstringで取得、DateManager 日付と体重の組を管理(コンストラクタで乱数組作成)
func getToDay(day : Double = 0) -> String {
let now = Date( timeInterval: 60*60*24*day, since: Date() as Date )
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: now as Date)
}
class DateManager {
var dateToWeight : Dictionary<String,String> = [:]
static let singleton = DateManager()
private init(){
for i in 10...20 {
dateToWeight[getToDay(day:(Double)(-i))] = String((Int)(arc4random()) % 50+50);
}
}
func saveWeight(day :String , weight : String){
dateToWeight[day] = weight;
}
func ToString() -> String {
var str : String = "";
for (key, value) in dateToWeight {
str += key + value + "\n";
}
return str;
}
func getWeight( day : String ) -> String {
if dateToWeight.contains(where:{
key,value in
return key.contains(day)}){
return dateToWeight[day]!;
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment