为什么我在调用中得到不正确的参数标签

时间:2017-10-24 03:49:25

标签: ios swift

获取“调用中的参数标签不正确(有'rest:',期望'restaurant:')”错误。这是代码。参数是正确的,我传递的是正确的类型?这是因为它们是类方法吗?

         class func save(restaurant: Restaurant, toCloud: Bool) -> Bool {
            var rest:RestaurantMO
            var saved:Bool = false
            if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
                rest = RestaurantMO(context: appDelegate.persistentContainer.viewContext)
                rest.name = restaurant.name
                rest.item = restaurant.item
                rest.location = restaurant.location
                rest.isVisited = restaurant.isVisited

                // Core Data Exercise - Solution
                rest.phone = restaurant.phone

                let entity =
                    NSEntityDescription.entity(forEntityName: "Restaurant",
                                               in: appDelegate.persistentContainer.viewContext)!
                _ = NSManagedObject(entity: entity,
                                             insertInto: appDelegate.persistentContainer.viewContext)

                print("Saving data to context ...")
                appDelegate.saveContext()
                saved = true

            }
            if toCloud {
                saveRecordToCloud(rest:RestaurantMO)  <--- ERROR: Incorrect argument label in call (have 'rest:', expected 'restaurant:')
            }

        }



         class func saveRecordToCloud(restaurant:RestaurantMO!) -> Void {

            // Prepare the record to save
            let record = CKRecord(recordType: "Restaurant")
            record.setValue(restaurant.name, forKey: "name")
            record.setValue(restaurant.item, forKey: "item")
            record.setValue(restaurant.location, forKey: "location")
            record.setValue(restaurant.phone, forKey: "phone")

            let imageData = restaurant.image! as Data

            // Resize the image
            let originalImage = UIImage(data: imageData)!
            let scalingFactor = (originalImage.size.width > 1024) ? 1024 / originalImage.size.width : 1.0
            let scaledImage = UIImage(data: imageData, scale: scalingFactor)!

            // Write the image to local file for temporary use
            let imageFilePath = NSTemporaryDirectory() + restaurant.name!
            let imageFileURL = URL(fileURLWithPath: imageFilePath)
            try? UIImageJPEGRepresentation(scaledImage, 0.8)?.write(to: imageFileURL)

            // Create image asset for upload
            let imageAsset = CKAsset(fileURL: imageFileURL)
            record.setValue(imageAsset, forKey: "image")

            // Get the Public iCloud Database
            let publicDatabase = CKContainer.default().publicCloudDatabase

            // Save the record to iCloud
            publicDatabase.save(record, completionHandler: { (record, error) -> Void  in
                // Remove temp file
                try? FileManager.default.removeItem(at: imageFileURL)
            })
        }

1 个答案:

答案 0 :(得分:0)

  

参数正确,我传递的是正确的类型

不,两者都错了。

该方法声明为

class func saveRecordToCloud(restaurant:RestaurantMO!)
  • 参数标签为restaurant而非rest(错误消息明确说明)
  • 参数类型为RestaurantMO,而不是RestaurantMO.type(预期的实例)

正确的语法是

saveRecordToCloud(restaurant: rest) 

注意:由于参数是非可选的,请删除方法声明中的感叹号:

func saveRecordToCloud(restaurant:RestaurantMO)