我正在尝试将整数保存到Core Data但是当我查询该值时,我总是得到“0”。
import UIKit
import CoreData
class RegisterElgin: UIViewController, UIPickerViewDelegate {
var navBar:UINavigationBar=UINavigationBar()
var nameID: String!
@IBOutlet var elginPositionLabel: UILabel!
@IBOutlet var saveButton: UIButton!
var position: String!
var positionNumber: NSNumber!
let elginPosition = ["N/A", "Lifeguard", "Instructor", "Supervisor", "Lifeguard & Instructor", "Lifeguard, Instructor, & Supervisor"]
override func viewDidLoad() {
super.viewDidLoad()
setNavBarToTheView()
addText()
println(nameID)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setNavBarToTheView()
{
navBar.frame=CGRectMake(0, 0, 375, 64)
self.view .addSubview(navBar)
}
func addText ()
{
var label = UILabel(frame:CGRectMake(2, 10, 375, 64))
//label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "Elgin West"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(label)
}
func numberOfComponentsInPickerView(PickerView: UIPickerView!) -> Int
{
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
{
return elginPosition.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
{
return elginPosition[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//elginPositionLabel.text = elginPosition[row]
position = elginPosition[row]
}
@IBAction func buttonTapped(sender: AnyObject) {
if (position == "Instructor")
{
positionNumber = 2
}
else
{
positionNumber = 4
}
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var savePosition = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject
savePosition.setValue(positionNumber, forKey: "elginPosition")
println (positionNumber)
context.save(nil)
}
}
当我去提取值时:
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false
var results: NSArray = context.executeFetchRequest(request, error: nil)!
if(results.count > 0)
{
var res = results [0] as NSManagedObject
userID = res.valueForKey("userID") as String
position = res.valueForKey("elginPosition") as NSNumber
println (position)
}
我得到'O'。
另外,我尝试将Core Data模型设置为Integer 16,32和64。
答案 0 :(得分:0)
我写这段代码很久以前保存到核心数据,这节省了多个字段的实体
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: managedContext)
let note = NSManagedObject(entity: entity!, insertIntoManagedObjectContext:managedContext)
note.setValue(text, forKey: "text")
note.setValue(hour, forKey: "inTheNext")
var error: NSError?
if !managedContext.save(&error)
{
println("Could not save \(error), \(error?.userInfo)")
}
这是获取它的代码:
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"Note")
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
if let results = fetchedResults
{
self.notes = results
}
else
{
println("Could not fetch \(error), \(error!.userInfo)")
}
获取代码获取所有值(不使用查询),我看到你没有使用查询,所以这应该可以帮助你