Swift - NSIndex forRow inSection Initializer

I have the following code:

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

and I get a compiler warning:

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 to do something like:

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

What i don't understand 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++;
        }
+4
source share
3 answers

indexmay not be recognized by the compiler. Try the code on the playground:

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

Job

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

Error: Extra Argument 'inSection' in call

This means that your variable is indexnot defined. Or you should try Cleanand remove the derived data.

: index dispatch_async, index.

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);
                });

: Swift . ( + ):

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);
    }

: . dispatch_async, { index in, , . ( ). .

    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++
    }
+8

index , . .

var index = 0

for (key, value) in map! {
    { (idx) in // <- this is fixed value `index`
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            // some code...
            dispatch_async(dispatch_get_main_queue()) {
                var path = NSIndexPath(forRow: idx, inSection:0);
                self.tableView.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimation.Automatic);
            }
        }
    }(index++)
}

OP JavaScript:

var index = 0;
while(...) {
  dispatch_async(GLOBAL_QUEUE, function(index) {            

    //some code

    dispatch_async(MAIN_QUEUE, function(index) {
      var path = new NSIndexPath(index, 0);
      self.tableView.reloadRowsAtIndexPaths([path], ANIM_AUTO);
    });
  });

  index++;
}

? dispatch_async , index undefined.

:

var index = 0;
while(...) {
  (function(idx) {
    dispatch_async(GLOBAL_QUEUE, function() {            

      //some code

      dispatch_async(MAIN_QUEUE, function() {
        var path = new NSIndexPath(idx, 0);
        self.tableView.reloadRowsAtIndexPaths([path], ANIM_AUTO);
      });
    });
  })(index++);
}

, JavaScript, ?

+2

For me, Xcode 8.3 and Swift 3 work as shown below:

  let indexPath = NSIndexPath(row: row, section: section)
+1
source

All Articles