iOS可选包装在Swift中

时间:2015-09-08 09:24:39

标签: ios swift optional wrapping

我将一个数组保存到一个模型中,当保存数据时没有用Optional(...)包装,但是当读取数据时,我得到了包围它的Optional(...)。 感谢您的帮助。

 saveOperativesInModel: Test Name
 saveOperativesInModel: test team 999

 getOperativesFromModel: Optional(Test Name)
 getOperativesFromModel: Optional(test team 999)



func saveOperativesInModel() {

    if (self.operativesResult?.operativesList.count > 0) {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!
        let entity =  NSEntityDescription.entityForName("Operatives", inManagedObjectContext: managedContext)

        // Remove all records in entity
        let fetchRequest = NSFetchRequest()
        fetchRequest.entity = entity
        fetchRequest.includesPropertyValues = false

        var error:NSError?
        if let results = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject] {
            for result in results {
                managedContext.deleteObject(result)
            }
        }

        var item: NSManagedObject!
        //var itemInArray: Operative!
        if let operativesList = self.operativesResult?.operativesList {
            self.operativesTable.removeAll()
            for itemInArray in operativesList {
                item = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)
                item.setValue(itemInArray.id, forKey: "id")
                item.setValue(itemInArray.firstName, forKey: "firstName")
                item.setValue(itemInArray.lastName, forKey: "lastName")
                item.setValue(itemInArray.mobile, forKey: "mobile")
                var error: NSError?
                if !managedContext.save(&error) {
                    println("saveOperativesInModel - Could not save \(error), \(error?.userInfo)")
                } else {
                    self.operativesTable.append(item!)
                    println("saveOperativesInModel: \(itemInArray.firstName)")
                }
            }
        }
        //println("saveOperativesInModel : \(operativesTable.count) items")

        let fetchRequest1: NSFetchRequest! = NSFetchRequest(entityName:"Operatives")
        var error1: NSError?
        let fetchedResults = managedContext.executeFetchRequest(fetchRequest1, error: &error1) as? [NSManagedObject]
        if let operativesTable = fetchedResults {
            if operativesTable.count > 0 {
                println("getOperativesFromModel : \(operativesTable.count)")
            }
            if operativesTable.count > 0 {
                for item in operativesTable {
                    let operative: Operative! = Operative()
                    operative.id = String (stringInterpolationSegment: item.valueForKey("id"))
                    operative.firstName = String (stringInterpolationSegment: item.valueForKey("firstName"))
                    operative.lastName = String (stringInterpolationSegment: item.valueForKey("lastName"))
                    println("getOperativesFromModel: \(operative.firstName)")
                }
            }
        } else {
            println("Could not fetch \(error1), \(error1!.userInfo)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

来自firstName类的Operative属性应该是可选的(可能声明为var firstName: String?。所以如果你确定firstName中有一个字符串,你可以替换你的打印与此相符:

println("getOperativesFromModel: \(operative.firstName!)")