我的代码有什么问题(Cloud Kit)

时间:2016-03-03 01:54:36

标签: xcode swift

因此,这是我的代码,允许警报和警报内写入的文本保存在云端。我实际上是在关注教程https://www.youtube.com/watch?v=XQ3nLV2778U 我在我的一些代码上收到2条错误消息!

  1. 错误:任何对象与CKRecords(位于self.post = post)
  2. 不同
  3. 错误:CKRecords没有名为subscript的成员(位于newPost [" content"]
  4. 我编写的代码与教程在教程中完成的方式完全相同,只是我有错误消息而且他没有!我该如何解决这些问题?

        var posts = [CKRecord]()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
    }
    
    func loadData() {
        posts = [CKRecord]()
    
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        let query = CKQuery(recordType: "Post", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
        publicData.performQuery(query, inZoneWithID: nil) { (CKRecord, NSError) -> Void in
            if let posts = CKRecord{
                self.posts = posts //Error "Any Object is not Identical to CKRecords"
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.tableView.reloadData()
                })
            }
    
        }
    }
    
    @IBAction func createPost(sender: AnyObject) {
    
        let alert = UIAlertController(title: "New Post", message: "Enter New Post", preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler { (textField:UITextField!) -> Void in
            textField.placeholder = "Your Post"
        }
        alert.addAction(UIAlertAction(title: "Post", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> Void in
            let textField: AnyObject = alert.textFields!.first!
    
            if textField.textLabel??.text != "" {
                let newPost = CKRecord(recordType: "Post")
                newPost["content"] = textField.textLabel??.text //ERROR "CKRecord does not have a member named subscript"
    
    
    
                let publicData = CKContainer.defaultContainer().publicCloudDatabase
                publicData.saveRecord(newPost, completionHandler: { (record: CKRecord?, error: NSError?) -> Void in
                    if error == nil {
                        print("Post Saved")
                    }
                })
    
            }
    
    
    
    
    
    
        }))
    
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    

1 个答案:

答案 0 :(得分:0)

将您的Xcode更新到最新版本。

此语法从特定版本的Swift开始提供。我不记得你需要哪个版本,但如果你只是更新Xcode,你的问题就会消失。