Array statements giving Swift2 id error in Swift4

Asked 2 years ago, Updated 2 years ago, 66 views

Based on Swift2's book, I created the following code in Swift4 and found the following error in the array part that gives the id:Could you tell me the solution?

Cannot convert value of type 'String' to expected argument type' (Diary, Diary) flows ->Bool'


diary.id=dataArray.max(by: "id")!+1

import UIKit
import RealmSwift

classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    Called when screen transitions in //segue
    override func prepare(for segment:UIStoryboardSegue, sender:Any?) {
        let inputViewController: inputViewController=segue.destination as! inputViewController
        if segue.identifier == "cellsegue" {
            let indexPath=self.tableView.indexPathForSelectedRow
            inputViewController.diary=dataArray [indexPath!.row]

        } else{
            let diary=Diary()
            diary.title="Title"
            diary.body="body"
            if dataArray.count!=0{

                // Error part
                *** diary.id= dataArray.max(by: "id")!+1***

            }
            inputViewController.diary=diary
        }
    }
    // Cannot convert value of type 'String' to expected argument type' (Diary, Diary) flows ->Bool'

swift realm swift4

2022-09-29 22:16

1 Answers

Invalid method used. max(by:) is the standard library method that takes the closure as an argument and returns the largest element of the collection.

The aggregate function that returns the maximum properties of Realm is max(ofProperty:).The code for that part needs to be written as follows.

diary.id=dataArray.max(ofProperty: "id")!+1


2022-09-29 22:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.