Unused

I can’t get the _unused attribute to work properly.

Every time I try and run the code below I get a message saying “Use of undeclared identifier “_unused””.

Did something change in the new version of iOS, or am I entering this information wrong?

Thanks

#import <Foundation/Foundation.h>
#import “BNRLogger.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    BNRLogger *logger = [[BNRLogger alloc]init];
    _unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                      target:logger
                                                    selector:@selector(updateLastTime:)
                                                    userInfo:nil
                                                     repeats:YES];
    [[NSRunLoop currentRunLoop] run];
    
}
return 0;

}

[quote]Did something change in the new version of iOS, or am I entering this information wrong?
[/quote]
Check your typing; you missed the second ‘_’ character.

should be:

__unused NSTimer *timer = ...

To clarify - the __unused modifier is used where an object isn’t called by another object?

Intuitively one might not think of the NSTimer object in this chapter to be unused, given that it performs an action after its time interval.

When you invoke a method that has a return value, but don’t actually store that return value in a variable for your own use, the compiler thinks you’ve made a mistake and will emit a warning. The __unused keyword silences this warning, telling the compiler that you’ve intentionally decided not to consume the return value of the message into a variable.

1 Like