Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - NSIndex forRow inSection Initializer

Tags:

ios

swift

I have the following code:

var path = NSIndexPath(forRow: index, inSection:0);

and I get a compiler warning saying:

Extra Argument 'inSection' in call

Why? I see the initializer in the documentation here:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSIndexPath_UIKitAdditions/index.html#//apple_ref/occ/clm/NSIndexPath/indexPathForRow:inSection:

I imported UIKit, and even tried doing something like:

var path = UIKit.NSIndexPath(forRow: index, inSection:0);

What am I misunderstanding here?

EDIT:

var index = 0;

        for (key, value) in map! {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { (index) in
                //some code

                dispatch_async(dispatch_get_main_queue(), { (index) in
                    println("reloadRow row=" + String(index) + ",inSection:0");
                    var path = NSIndexPath(forRow: index, inSection:0);
                    self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimation.Automatic);
                });
            });
            index++;
        }
like image 982
thatidiotguy Avatar asked Dec 19 '22 10:12

thatidiotguy


1 Answers

index may not be recognized by the compiler. Try the code in playground:

import UIKit
let index = 3
var path = NSIndexPath(forRow: index, inSection: 0)

Works

import UIKit
var path = NSIndexPath(forRow: index, inSection: 0)

Error: Extra Argument 'inSection' in call

It means that your index variable is not well defined. Or you should try to Clean and delete the derived data.

Edit: Remove the index declaration in your dispatch_async it conflicts with your index variable.

dispatch_async(dispatch_get_main_queue(), { _ in
                    println("reloadRow row=" + String(index) + ",inSection:0");
                    var path = NSIndexPath(forRow: index, inSection:0);
                    self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimation.Automatic);
                });

Bonus: You should use Swift nice shortcuts with closures. (trailing closure + no parameters) as it:

dispatch_async(dispatch_get_main_queue()) {
       println("reloadRow row=" + String(index) + ",inSection:0");
       var path = NSIndexPath(forRow: index, inSection:0);
       self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimation.Automatic);
    }

Edit: you do the same mistake in the first dispatch async. When you declare a dispatch_async and you write { index in it's the same thing as declaring a function with an index as a parameter. It's a variable scope issue (you should read about this). I update the final code.

    var index = 0
    for (key, value) in map! {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            //some code
            dispatch_async(dispatch_get_main_queue()) {
                println("reloadRow row=" + String(index) + ",inSection:0");
                var path = NSIndexPath(forRow: index, inSection:0);
                self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimation.Automatic);
            }
        }
        index++
    }
like image 127
Francescu Avatar answered Jan 10 '23 04:01

Francescu