user10063119
user10063119

Reputation:

+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class

I am trying to resolve an alias file's original path using Objective-C(or maybe C++; it's an .mm file). Not being very much familiar, I am somehow missing + and - methods' usage. I am aware of them being class and instance methods respectively, but in practice, the following the code, with the indicated lines give me following warning and error(at build):

Class method '+bookmarkDataWithContentsOfURL:' not found (return type defaults to 'id')

-

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class 0x7fff88942cb8'

with 0x7fff88942cb8 being the NSData address as per lldb.

Which files should I make the changes in, to get bookmarkDataWithContentsOfURL:error: and URLByResolvingBookmarkData to work?

void *pathclass::resolveAliasFromURL(const char *filepath) const
{
  NSError *error = nil;
  NSString *filepathh = [[NSString alloc] initWithUTF8String:filepath];
  NSData *bookmarkk = [NSData bookmarkDataWithContentsOfURL:filepathh]; /*problematic line*/


  BOOL isstale = NO;
  NSURL *actual = [NSURL URLByResolvingBookmarkData:bookmarkk bookmarkDataIsStale:isstale error:error];/*another problematic line, but build fails already*/
  NSString *urlString = [actual absoluteString];
  NSLog(@"%@",urlString);
}

If there are any other faults, please point out.

Upvotes: 0

Views: 296

Answers (1)

TheNextman
TheNextman

Reputation: 12566

Your call to bookmarkDataWithContentsOfURL: is wrong in a few ways:

The signature looks like this:

+ (NSData *)bookmarkDataWithContentsOfURL:(NSURL *)bookmarkFileURL error:(NSError * _Nullable *)error;

First, the first parameter is of type NSURL*, not NSString*. Next, you miss off the error parameter completely (despite defining a variable for it). Lastly, the method is a class method on NSURL not NSData (NSData* is the return type).

So, first, make your file path into an NSURL*:

NSURL* bookmarkUrl = [NSURL URLWithString:filepathh];

Then, call the function using the proper arguments:

NSData *bookmarkk = [NSURL bookmarkDataWithContentsOfURL:bookmarkUrl error:&error];

You should check the returned value against nil - if it's nil, then an error occurred, and the error information will be contained inside error.

The documentation is quite helpful.

Your call to URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error: has similar problems: you are missing several parameters, the first parameter should be NSURL, etc. Again, the documentation should help.

Upvotes: 2

Related Questions