我对Swift很新,我正在尝试实现文件创建日期检查。
我们的想法是检查文件的创建时间是否超过7天。由于某种原因,代码总是返回“没有这样的文件”错误。
要查看错误,我使用相同的路径在同一个函数中读取文件的内容,并且完美无瑕。
我是否错误地使用了路径或者误解了某些内容?
func creationDateCheck(name: String) -> Bool {
// This is the path I am using, file is in the favorites folder in the .documentsDirectory
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let myFilesPath = documentsUrl.appendingPathComponent("favorites/" + name + ".xml")
let pathString = "\(myFilesPath)"
//First do block tries to get the file attributes and fails
do {
let fileAttributes = try FileManager.default.attributesOfItem(atPath: pathString)
print(fileAttributes)
let creationDate = (fileAttributes[FileAttributeKey.creationDate] as? NSDate)!
return daysBetweenDates(endDate: creationDate as Date) > 7
} catch let error as NSError {
print(error.localizedDescription)
}
// Second do block reads from file successfully using the same path
do {
print(try String(contentsOf: myFilesPath, encoding: String.Encoding.utf8))
}catch {print("***** ERROR READING FROM FILE *****")}
return false
}
答案 0 :(得分:5)
您无法直接使用$this->db->delete('crm_seller_cart',array('product_id' => $product_id,'seller_id'=>$loginSession[0]->seller_id));
$this->db->delete('crm_customer_recently_viewed',array('product_id' => $product_id,'seller_id'=>$loginSession[0]->seller_id));
从String
获取URL
,而无需使用"\(myFilesPath)"
的{{1}}属性。
path
您正在成功阅读文件内容的原因是您使用了URL
并且它将接受let fileAttributes = try FileManager.default.attributesOfItem(atPath: myFilesPath.path)
个对象作为第一个参数。