创建 NSManagedObject 的新实例 class
Creating a new instance of NSManagedObject class
我正在尝试创建 class Student 的新对象,但是当我调用函数 createnewStudent
说
时出现错误
Cannot invoke 'CreateStudent' with an argument list of type '(NSManagedObjectContext, combination: String, stream: String, name: String, DOB: NSDate, gender: UISegmentedControl!, DOA: NSDate, fathername: UITextField, mothername: UITextField, familyincome: Int, emailid: String, phonenumber: Int)’
我该如何继续?
这是我的 ViewController.swift 文件:
import UIKit
import CoreData
class PortalViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var coursePicker: UIPickerView!
@IBOutlet weak var combination: UILabel!
@IBOutlet weak var streamPrint: UILabel!
@IBOutlet weak var genderSelected: UISegmentedControl!
@IBOutlet weak var DOB: UIDatePicker!
@IBOutlet weak var studentName: UITextField!
@IBOutlet weak var fatherName: UITextField!
@IBOutlet weak var fathersIncome: UITextField!
@IBOutlet weak var mothersName: UITextField!
@IBOutlet weak var mothersIncome: UITextField!
@IBOutlet weak var phoneNumber: UITextField!
@IBOutlet weak var emailID: UITextField!
var newStudent = Student()
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let stream = ["Science","Arts","Commerce"]
let combinationScience = ["PCM", "PMCs", "CBZ"]
let combinationArts = ["HEP","English","Kannada"]
let combinationCommerce = ["BBM", "Economics","xyz"]
var selectedRow = 0
func pickerView(pickerView : UIPickerView , didSelectRow row : Int , inComponent component : Int){
switch component {
case 0 :
selectedRow = row
streamPrint.text = stream[row]
coursePicker.reloadAllComponents()
case 1 :
switch selectedRow {
case 0: combination.text = combinationScience[row]
case 1: combination.text = combinationArts[row]
case 2: combination.text = combinationCommerce[row]
default : combination.text = "combination"
}
default : selectedRow = 0
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
switch component {
case 0 : return stream[row]
case 1 :
switch selectedRow {
case 0: return combinationScience[row]
case 1: return combinationArts[row]
case 2: return combinationCommerce[row]
default: return nil
}
default: return nil
}
}
@IBAction func proceed(sender: AnyObject) {
var fi1 = fathersIncome.text.toInt()
var fi2 = mothersIncome.text.toInt()
var fi = fi1! + fi2!
var newStudent = Student.CreateStudent(managedObjectContext!, combination: combination.text!, stream: streamPrint.text!, name: studentName.text!, DOB: DOB.date, gender: genderSelected, DOA: DOB.date, fathername: fatherName!, mothername: mothersName!, familyincome: fi, emailid: emailID.text!, phonenumber: phoneNumber.text.toInt()!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我的 NSManagedObject 文件
导入基金会
导入核心数据
class Student: NSManagedObject {
@NSManaged var combination: String
@NSManaged var currentSemester: NSNumber
@NSManaged var dateOfAdmition: NSDate
@NSManaged var dateOfBirth: NSDate
@NSManaged var emailID: String
@NSManaged var familyIncome: NSNumber
@NSManaged var fatherName: String
@NSManaged var gender: String
@NSManaged var motherName: String
@NSManaged var name: String
@NSManaged var phoneNumber: NSNumber
@NSManaged var registerNumber: NSNumber
@NSManaged var stream: String
class func CreateStudent(moc : NSManagedObjectContext, combination : String, stream : String, name : String , DOB : NSDate, gender : String, DOA : NSDate,fathername :String, mothername: String, familyincome : Int , emailid : String, phonenumber : Int)-> Student{
let newStudent = NSEntityDescription.insertNewObjectForEntityForName("Student", inManagedObjectContext: moc) as! Student
newStudent.combination = combination
newStudent.stream = stream
newStudent.name = name
newStudent.dateOfBirth = DOB
newStudent.dateOfAdmition = DOA
newStudent.fatherName = fathername
newStudent.motherName = mothername
newStudent.familyIncome = familyincome
newStudent.emailID = emailid
newStudent.phoneNumber = phonenumber
return newStudent
}
}
当您应该传递 String
值时,您为性别参数传递了 UISegmentedControl
实例。
在传递 UITextField
个实例时,还要检查 fatherName
和 motherName
。
我正在尝试创建 class Student 的新对象,但是当我调用函数 createnewStudent
说
Cannot invoke 'CreateStudent' with an argument list of type '(NSManagedObjectContext, combination: String, stream: String, name: String, DOB: NSDate, gender: UISegmentedControl!, DOA: NSDate, fathername: UITextField, mothername: UITextField, familyincome: Int, emailid: String, phonenumber: Int)’
我该如何继续?
这是我的 ViewController.swift 文件:
import UIKit
import CoreData
class PortalViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var coursePicker: UIPickerView!
@IBOutlet weak var combination: UILabel!
@IBOutlet weak var streamPrint: UILabel!
@IBOutlet weak var genderSelected: UISegmentedControl!
@IBOutlet weak var DOB: UIDatePicker!
@IBOutlet weak var studentName: UITextField!
@IBOutlet weak var fatherName: UITextField!
@IBOutlet weak var fathersIncome: UITextField!
@IBOutlet weak var mothersName: UITextField!
@IBOutlet weak var mothersIncome: UITextField!
@IBOutlet weak var phoneNumber: UITextField!
@IBOutlet weak var emailID: UITextField!
var newStudent = Student()
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let stream = ["Science","Arts","Commerce"]
let combinationScience = ["PCM", "PMCs", "CBZ"]
let combinationArts = ["HEP","English","Kannada"]
let combinationCommerce = ["BBM", "Economics","xyz"]
var selectedRow = 0
func pickerView(pickerView : UIPickerView , didSelectRow row : Int , inComponent component : Int){
switch component {
case 0 :
selectedRow = row
streamPrint.text = stream[row]
coursePicker.reloadAllComponents()
case 1 :
switch selectedRow {
case 0: combination.text = combinationScience[row]
case 1: combination.text = combinationArts[row]
case 2: combination.text = combinationCommerce[row]
default : combination.text = "combination"
}
default : selectedRow = 0
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
switch component {
case 0 : return stream[row]
case 1 :
switch selectedRow {
case 0: return combinationScience[row]
case 1: return combinationArts[row]
case 2: return combinationCommerce[row]
default: return nil
}
default: return nil
}
}
@IBAction func proceed(sender: AnyObject) {
var fi1 = fathersIncome.text.toInt()
var fi2 = mothersIncome.text.toInt()
var fi = fi1! + fi2!
var newStudent = Student.CreateStudent(managedObjectContext!, combination: combination.text!, stream: streamPrint.text!, name: studentName.text!, DOB: DOB.date, gender: genderSelected, DOA: DOB.date, fathername: fatherName!, mothername: mothersName!, familyincome: fi, emailid: emailID.text!, phonenumber: phoneNumber.text.toInt()!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我的 NSManagedObject 文件 导入基金会 导入核心数据
class Student: NSManagedObject {
@NSManaged var combination: String
@NSManaged var currentSemester: NSNumber
@NSManaged var dateOfAdmition: NSDate
@NSManaged var dateOfBirth: NSDate
@NSManaged var emailID: String
@NSManaged var familyIncome: NSNumber
@NSManaged var fatherName: String
@NSManaged var gender: String
@NSManaged var motherName: String
@NSManaged var name: String
@NSManaged var phoneNumber: NSNumber
@NSManaged var registerNumber: NSNumber
@NSManaged var stream: String
class func CreateStudent(moc : NSManagedObjectContext, combination : String, stream : String, name : String , DOB : NSDate, gender : String, DOA : NSDate,fathername :String, mothername: String, familyincome : Int , emailid : String, phonenumber : Int)-> Student{
let newStudent = NSEntityDescription.insertNewObjectForEntityForName("Student", inManagedObjectContext: moc) as! Student
newStudent.combination = combination
newStudent.stream = stream
newStudent.name = name
newStudent.dateOfBirth = DOB
newStudent.dateOfAdmition = DOA
newStudent.fatherName = fathername
newStudent.motherName = mothername
newStudent.familyIncome = familyincome
newStudent.emailID = emailid
newStudent.phoneNumber = phonenumber
return newStudent
}
}
当您应该传递 String
值时,您为性别参数传递了 UISegmentedControl
实例。
在传递 UITextField
个实例时,还要检查 fatherName
和 motherName
。