我在我的应用程序开发中遇到了一个愚蠢的问题好几个小时...... 有人可以帮我解决这个问题吗?
除了" game_releasedate"以外我没有打印所有数据的问题。数据是"日期"数据
let foundgame = GameClass()
//Look into Database
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Game_BDD")
request.predicate = NSPredicate(format: "game_barcode == %@", barcode)
print (barcode)
//Autofill the game name, console, region, etc...
do {
let result = try context.fetch(request)
print (result.count)
if result.count > 0 {
foundgame.game_Name = (result[0] as AnyObject).value(forKey: "game_name") as! String
foundgame.game_Console = (result[0] as AnyObject).value(forKey: "game_console") as! String
foundgame.game_Genre = (result[0] as AnyObject).value(forKey: "game_genre") as! String
foundgame.game_Publisher = (result[0] as AnyObject).value(forKey: "game_publisher") as! String
foundgame.game_Region = (result[0] as AnyObject).value(forKey: "game_country") as! String
let date = (result[0] as AnyObject).value(forKey: "game_releasedate") as! NSDate
print(date)
}
else
{
foundgame.game_Name = "Barcode Not found"
}
}
catch{
print("Error looking in the DB")
}
谢谢
编辑:
这就是我将游戏数据放入数据库的方式:
let formatdate = DateFormatter()
formatdate.dateFormat = "dd/mm/yyyy"
print(gametxt[3])
let newGame = NSEntityDescription.insertNewObject(forEntityName: "Game_BDD", into: context) as NSManagedObject
newGame.setValue(gametxt[0], forKey: "game_console")
newGame.setValue(gametxt[1], forKey: "game_genre")
newGame.setValue(gametxt[2], forKey: "game_name")
newGame.setValue(gametxt[3], forKey: "game_country")
newGame.setValue(gametxt[4], forKey: "game_publisher")
newGame.setValue(gametxt[5], forKey: "game_productid")
newGame.setValue(gametxt[6], forKey: "game_barcode")
newGame.setValue(formatdate.date(from: gametxt[7]), forKey: "game_releasedate")
答案 0 :(得分:1)
将日期更改为有效日期:
//如果您的日期为Date
格式
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
print(dateFormatter.string(from: date)) // outputs : 17/09/2017
//如果您的日期为String
格式
let dateString = "04/14/97"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "mm/dd/yy"
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd/mm/yyyy"
print(dateFormatter.string(from: date!)) // outputs : 14/04/1997