-4

I am working on an app and want to make a delete option where you swipe right and click delete to delete a row. I am getting an "Expected Declaration" error and I've tried to fix it for about 20 minutes.

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var StoredValues = Values()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "meunSegue", sender: self)

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            _ = segue.destination as! SecondViewController
        }
        class SecondViewController: UIViewController {

            var recievedData = ""
            override func viewDidLoad() {
                super.viewDidLoad()
                print(recievedData)
            }
        }
    }
    - (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {
    [maTheData removeObjectAtIndex: [indexPath row]];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];


    }
}
3
  • Your code is all messed up. You have an ObjC function (missing a closing bracket) mixed with Swift and a function AND a class inside another function. You also forgot to tell us what line the error is thrown in, we're not compilers. May 11, 2017 at 21:59
  • Sorry about that The error is in these lines
    – Chris Pine
    May 12, 2017 at 2:34
  • - (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) { [maTheData removeObjectAtIndex: [indexPath row]];
    – Chris Pine
    May 12, 2017 at 2:34

2 Answers 2

1

You are mixing

- (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {
[maTheData removeObjectAtIndex: [indexPath row]];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

That looks like Objective C, while rest of your class is in Swift. It is easy to mixup, when you use multiple languages, but compiler does pretty good job for helping you in cases like this.

1

There is a method inside the method rather than calling the method. In other words, func inside the func . Even further, I do not know how would you even possible to put a class inside a func? As Sandeep has mentioned you are also mixing up Objective C withSwift.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "meunSegue", sender: self)

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            _ = segue.destination as! SecondViewController
        }
        class SecondViewController: UIViewController {

            var recievedData = ""
            override func viewDidLoad() {
                super.viewDidLoad()
                print(recievedData)
            }
        }
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.