-3

I read 5 other related questions with the same error or syntax. As far as I understood non had anything related to my question.

-(void) createCopyOfDBIfNeeded{
    NSFileManager *fileManager= [NSFileManager defaultManager];

    BOOL isDatabaseInCache = [fileManager fileExistsAtPath:@selector(getDBFile)];

    if (isDatabaseInCache) {
        return;
    }
}

//getDBFile Method:

-(NSString *) getDBFile {
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString * DBPath = [paths firstObject];
    NSString * DBFile = [DBPath stringByAppendingPathComponent:@"productDBFile.db"];
    return DBFile;

}

It seems the error is related to @selector(getDBFile). If I use [self getDBFile] instead everything works, but I want to learn how and where to use @selector appropriately and what the error/warning means here.

I ALSO get a warning: Incompatible pointer types sending 'SEL' to parameter of type


EDIT: This is question is fundamentally a duplicate of What's the difference between a method and a selector?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 4
    You might want to read this: https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Selector.html – jtbandes Apr 25 '16 at 17:26
  • 2
    It looks like you have no idea of what a selector is, and possibly little understanding of functions and how to call them. Perhaps a good Objective-C primer is in order. – Avi Apr 25 '16 at 17:36
  • 1
    @asma22 Note that methods in Objective-C should never be prefixed with `get` unless they follow a very specific pattern of returning stuff by reference via the arguments. (Common point of confusion-- welcome to the language and have fun!). – bbum Apr 25 '16 at 19:41
  • 1
    @bbum (sensei) in a typography meetup I was told that it's better to avoid using `--` instead, you should use **Em Dash(—)** i.e. `Ctrl + shift + -` :]. Thanks for the welcome – mfaani Apr 25 '16 at 19:54
  • 1
    @asma22 Hah! You youngsters and your new fangled non-7 bit characters. :) In all seriousness, though, the setter/getter pair in objc is *always* `thing` and `setThing:`. And a read-only getter is just `thing`. Following the convention also makes use of `@property(readonly) NSString *thing;` *just work*. – bbum Apr 25 '16 at 23:22
  • Correction: for my previous comment I meant: `option + shift + -` – mfaani May 29 '16 at 22:10

1 Answers1

2

You can only pass a selector to a method that takes a selector. fileExistsAtPath: takes an NSString *, not a SEL.

It looks like you want to pass the return value of getDBFile to fileExistsAtPath:, not getDBFile's selector.

If so, just call getDBFile instead of taking its selector:

BOOL isDatabaseInCache = [fileManager fileExistsAtPath:[self getDBFile]];
NSGod
  • 22,699
  • 3
  • 58
  • 66
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
  • can u elaborate about your first line, I am passing a `SEL` which is returning a string, why is different from ur answer? Do u mean the method must be able to accept have some sort of a `performSelector` input option? How do I know if a method accepts such or not? – mfaani Apr 25 '16 at 17:25
  • @asma22 You are passing the method itself to something that expects the result of a method. – NobodyNada Apr 25 '16 at 17:30
  • 2
    @asma22 The system expects you to pass it the filename, but instead you're passing instructions to get the filename, which it doesn't expect. My example follows the instructions to get the filename, then passes the result to the system. Does that make sense? – NobodyNada Apr 25 '16 at 17:31
  • I get it that its not expecting one...I don't understand when would some method be expecting one. :] – mfaani Apr 25 '16 at 17:42
  • @asma22 An example would be `NSTimer`, which runs a selector after a specified period of time. – NobodyNada Apr 25 '16 at 17:43
  • I mean in general, I think to understand the advantage of performSelector I must better understand runtime vs compile time right? – mfaani Apr 25 '16 at 17:44
  • 1
    @asma22 Selectors are useful when some code needs to be able to run some other piece of code, without knowing exactly what that other code is at compile time. – NobodyNada Apr 25 '16 at 17:45