2017-10-24 16 views
-5

「コール中に不適切な引数ラベルがあります(rest: '、expected' 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) 
      }) 
     } 
+3

'saveRecordToCloud(レストラン:REST)' – WeiJay

+0

残りはparamはに渡されるパラメータは、非オプションであるようにメソッド宣言に感嘆符を削除しますか? – jdog

答えて

0

両方が間違っている、パラメータが正しく、私は正しいタイプ

ありませんを渡しています。

方法は、(エラーメッセージによって明確に述べ)

  • パラメータタイプはRestaurantMOなくRestaurantMO.typeある(インスタンスが期待されるパラメータラベルがrestaurantなくrestある

    class func saveRecordToCloud(restaurant:RestaurantMO!) 
    
    • として宣言され)

    正しい構文は

    です。
    saveRecordToCloud(restaurant: rest) 
    

    func saveRecordToCloud(restaurant:RestaurantMO) 
    
  • 関連する問題