0

I'm trying to import/use this project in my Swift application, but the compiler throws tons of error with following message:

No known class method for selector 'indexPathForRow:inSection:'

Following is the typical code which generates the above error:

if ([self tileForIndexPath:[NSIndexPath indexPathForRow:row inSection:i]].empty) {
...
}

I can able to run the downloaded project as an standalone application without having any problem, though.

I've also added all the frameworks to my main application - those were used and installed to the said project, but that didn't help. My main project's deployment target is 10.0.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Santanu Karar
  • 1,034
  • 1
  • 12
  • 27
  • It's a Swift application you're building, but that code is Objective-C. Are you sure you have the bridging headers in your project? – christopherdrum May 10 '18 at 07:28
  • Yes, I'm using Objc-bridge and imported the main class file from the project - #import "F3HNumberTileGameViewController.h" – Santanu Karar May 10 '18 at 07:36
  • 1
    The error is because of `[NSIndexPath indexPathForRow:row inSection:i]]` NSIndexPath's function `indexPathForRow:row` has to be called on an instance variable. it cant be called like a class/static function – Scriptable May 10 '18 at 08:06
  • But this is the exact copy of code files from the project. I'm wondering how this works there! – Santanu Karar May 10 '18 at 08:59

1 Answers1

1

indexPathForRow:inSection: is a static constructor method that creates NSIndexPath instances. It is defined in UIKit:

https://developer.apple.com/documentation/foundation/nsindexpath/1614934-indexpathforrow?language=objc

In this project the author imports UIKit in the pch file:

https://github.com/austinzheng/iOS-2048/blob/7c0840a0f7bd77b01d6a36778a253f8f4b2e6529/NumberTileGame/NumberTileGame/NumberTileGame-Prefix.pch#L14

So you have 2 options: either setup a pch file for Objective-C code in your project (create the file and add to Xcode project build settings), or in each source file where you get this error add #import <UIKit/UIKit.h>.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • Thank you! Adding the #import statement fixed all the compiler errors! Thank you for pointing to this. I tried to add UIKit.framework earlier to my project explicitly, but that didn't helped. – Santanu Karar May 10 '18 at 12:30
  • 1
    Nice. If you are making a UI app the UIKit is likely already there, in any case it affects linking, and #import affects compiling (see https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work ) – battlmonstr May 10 '18 at 12:36