请记住,这是我第一次使用本网站。
我是编码的完全初学者,不知何故,他最终负责为高中水平竞赛创建应用程序。我的知识非常有限,我发现很难找到关于如何使用CloudKit数据库的明确教程。
我的申请特定部分的任务是创建一个"创建个人资料"页面,用户可以输入信息,然后将其存储并显示在应用程序的其他ViewControllers中。
与数据库相关的大多数代码都是由教师提供的(从另一组的项目中取出),但没有解释它的使用。所以,我只是试图复制它并使其适应我的能力。
但是,在运行模拟器并创建测试配置文件时,单击保存按钮后不会发生任何操作。
我目前的代码如下:
import UIKit
import CloudKit
class ProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let database = CKContainer.defaultContainer().publicCloudDatabase
@IBOutlet weak var profileType: UISegmentedControl!
@IBOutlet weak var descriptionTextfield: UITextField!
@IBOutlet weak var nameTextfield: UITextField!
@IBOutlet weak var addressTextfield: UITextField!
@IBOutlet weak var emailTextfield: UITextField!
@IBOutlet weak var passwordTextfield: UITextField!
@IBOutlet weak var phoneTextfield: UITextField!
@IBOutlet weak var saveButton: UIButton!
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
}
@IBAction func saveProfile(sender: AnyObject) {
let newrecord = CKRecord(recordType: "PotLuck")
newrecord.setObject(nameTextfield.text as CKRecordValue?, forKey: "CompanyName")
newrecord.setObject(addressTextfield.text as CKRecordValue?, forKey: "CompanyLocation")
newrecord.setObject(emailTextfield.text as CKRecordValue?, forKey: "CompanyEmail")
newrecord.setObject(passwordTextfield.text as CKRecordValue?, forKey: "CompanyPassword")
newrecord.setObject(phoneTextfield.text as CKRecordValue?, forKey: "CompanyPhone")
switch profileType.selectedSegmentIndex
{
case 0:
newrecord.setObject("Business" as CKRecordValue?, forKey: "CompanyType")
case 1:
newrecord.setObject("Charity" as CKRecordValue?, forKey: "CompanyType")
default:
break;
}
database.saveRecord(newrecord, completionHandler: {newrecord, error in
if error != nil {
print("An error occured")
} else {
let newview = URLViewController(nibName: "URLViewControler", bundle: nil)
self.presentViewController(newview, animated: true, completion: nil)
}
}
)
}
是否有人能指出遗失或不正确的内容?