将NSDate和双打保存到实体(CoreData)

时间:2017-05-16 03:22:47

标签: ios swift core-data

我可以使用Strings让它工作得很好但是如果我尝试使用双打或NSDates那么我会收到错误:
"不能分配类型" NSDate的值?"键入" String?"
"不能分配类型" Double的值?"键入" String?"

@IBAction func save(_ sender: Any) {

    if item != nil {

        item?.startdate = startDate.text
        item?.pickup = pickup.text
        item?.miles = miles.text
        item?.company = company.text
        item?.destination = destination.text
        item?.enddate = endDate.text            

    } else {

        let entitydescription = NSEntityDescription.entity(forEntityName: "Entity", in: pc)

        let item = Entity(entity: entitydescription!, insertInto: pc)

        item.startdate = startDate.text
        item.pickup = pickup.text
        item.miles = miles.text
        item.company = company.text
        item.destination = destination.text
        item.enddate = endDate.text

    }

    do {

        try pc.save()

    } catch {

        print(error)
        return
    }

    navigationController!.popViewController(animated: true)

}

以下是每个字段的类型:
    @NSManaged public var startdate:NSDate?
    @NSManaged public var pickup:String?
    @NSManaged public var miles:Double
    @NSManaged public var company:String?
    @NSManaged public var destination:String?
    @NSManaged public var enddate:NSDate?

3 个答案:

答案 0 :(得分:0)

您需要将miles.text,startdate.text和enddate.text分别转换为Double,NSDate和NSDate。

适用于startdate.text / enddate.text:

我不确定在保存项目时您为这些值设置了多少限制,但是您应该使用默认值,只是因为转换失败。对于此示例,假设日期格式为“5/15/17”,格式为M / d / y

let defaultDate = NSDate() //current date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "M/d/y"

if let startDateText = startdate.text, let startDate = dateFormatter.string(from: startDateText) as? NSDate {
    item?.startdate = startDate
} else { 
    item?.startdate = defaultDate
}

if let endDateText = startdate.text, let endDate = dateFormatter.string(from: endDateText) as? NSDate {
    item?.startdate = endDate
} else { 
    item?.startdate = defaultDate
}

适用于miles.text:

同样的想法,根据item.miles的文本内容,使用默认值使转换失败

if let milesText = miles.text {
    item?.miles = Double(miles.text) ?? 0.0 //default is 0.0
} else { 
    item?.miles = 0.0
}

或简单的单行 -

item?.miles = Double(miles.text ?? "0.0")

如果您有一个特定的默认值,只需在分配项目属性之前声明它,

let defaultMilesStr = "0.432"
item?.miles = Double(miles.text ?? defaultMiles)

另外,只是提示,最好不要在分配值时将选项包裹起来。因此即使有检查以确保项目!= nil,使用“guard”或“if-let”安全地展开项目总体上更好。由于您正在创建一个新项目(如果不存在),在这种情况下,id将使用if-let:

if let item = item {

  // assign values to item's prop's
  // item.startdate = .....

} else if let entityDescription = NSEntityDescription.entity(forEntityName: "Entity", in: pc), let item = Entity(entity: entityDescription, insertInto: pc) {

  // assign values to item's prop's
  // item.startdate = .....

}

//further execution

答案 1 :(得分:0)

将您的日期字符串转换为NSdate,然后您就可以保存

尝试使用此代码我已提供您的日期字符串,您可以使用格式更改

let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MM-dd-yyyy"

答案 2 :(得分:0)

试试这个:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = /* date_format_you_want_in_string from
                            * http://userguide.icu-project.org/formatparse/datetime
                            */
let date = dateFormatter.date(from: /* your_date_string */)

并将其设置为:

item?.date = date

您可以使用以下英里:

if let dMiles = Double(miles.text!) {
    item?.miles.text = dMiles
} else {
    print("Not a valid Double: \(textField.text!)")
}

希望它能帮助!!