Mute NSAssert warning - objective-c

I compiled my project with -Weverything and I see that all the NSAssert calls have the warning
must specify at least one argument for '...' parameter of variadic macro
NSAssert(NO, #"This class cannot be instantiated!");
The correct fix would be to add a nil argument to all statements but I would prefer just to mute this warning. Is it possible?
cheers

Add the following parameters to your Other C flags in XCode build settings which turns off all NSAssert in your project:
-DNS_BLOCK_ASSERTIONS=1

Related

Get pointer to self in LLDB?

I want to create a breakpoint in xCode which uses LLDB and checks the current object class in condition section.
The problem is LLDB doesn't allow to use self to get a class. How to solve this problem? Maybe through other commands? For example, bt command output contains correct classname but it seems it is not allowed in LLDB too.
I presume this is related to:
Using of symbolic breakpoints for child classes in Xcode?
The problem comes if your breakpoint is in code with no debug information (like in system libraries.) The debugger knows nothing about self in this context, and you have to give it more help. In the case of self, you know that it was passed into a method call as the first argument, so you can use $arg1 to get at the value.

Suppress -Wproperty-no-attribute not working in Xcode

In an Xcode 5, ARC, iPhone project, I have received a large number of warnings reading "No 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed." I know what this warning means, and why I might want to fix it. But this would take a long time given the size of my project, so I instead want to suppress the warning. However, setting the -Wno-property-no-attribute flag on all of my .m files didn't fix anything, nor did setting the flag at Custom Compiler Flags > Other Warning Flags. I also tried #pragma GCC/clang diagnostic ignored "-Wproperty-no-attribute" before my property declarations, and have cleaned my build folder many times. The warnings persist. (Also, the -w flag did not work to suppress all warnings, when applied to my .m files.)
What should I do?
The pragma statement should be this: #pragma clang diagnostic ignored "-Wobjc-property-no-attribute"
It turned out that I had made a small typo in the Custom Compiler Flags entry (although I still do not know why the file-per-file flags didn't work), and that the LLVM warning about this was almost invisible, being swamped in the hundreds of other warnings generated.

KeychainItemWrapper Arcified

I've downloaded the latest version of KeyChainItemWrapper from GitHub, but when compiling it gives me this error: THIS CODE MUST BE COMPILED WITH ARC ENABLED!
So, I tried to Arcified my project by going to: Edit-->Refactor-->Convert to Objective-C ARC,
and run the converter, but it gives me the same error...
the origin of this error is from this code that exists in the KeyChainItemWrapper.m
#if ! __has_feature(objc_arc)
#error THIS CODE MUST BE COMPILED WITH ARC ENABLED!
#endif
How can I turn objc_arc feature ??
Thanks!
Go into build phases, Compile Sources, find the source for that file, double click and add "-fobjc-arc" for it's flags :). That will let you compile only that file with arc and use it in non-arc projects
This was posted a while, but I had the same issue so I'll post it here.
If you have ARC-enabled code already in your project when you convert, it will throw an error. Just comment out the lines so that they read:
/*#if ! __has_feature(objc_arc)
#error THIS CODE MUST BE COMPILED WITH ARC ENABLED!
#endif*/
Continue attempting to convert to ARC, and un-comment after it succeeds in converting.

Implicit declaration of function - C99

I am currently using Xcode 4, and in my .pch file I have this macro:
#define localize(s) NSLocalizedString((s), nil).
When I try to use this macro in some .m file, I receive this warning: Implicit declaration of function 'localize' is invalid in C99.
This code compiles without a problem, but how can I fix this so I don't get a warning?
I had this problem when I did a global replace of NSLog with DLog. I foolishly included the
#define DLog(...) NSLog(...
statements, so I ended up with
#define DLog(...) DLog(...
which caused the warnings, and a linker error.
Implicit function declarations are those that the compiler sees the first time used as a function call (as opposed to those where a prototype or the function definition is seen first).
Apparently your code used localize(foo) but the macro definition was not visible. Possible reasons: you forgot to #include the file containing the localize macro or the precompilation of headers went south an did not include the localize macro so it was left unexpanded.
Another "foolish" mistake I ran into was the fact that my DLog was defined in the prefix header of the iOS target, so I had to copy it over to the prefix of the OSX target, as well...
I had this problem because I accidentally imported CocoaLumberjack like this:
#import <CocoaLumberjack/DDLog.h>
Apparently the CocoaLumberjack team modularized the code some more; and macros like DDLogError are now defined separately in their own header file.
I replaced the import statement with this and the error went away:
#import <CocoaLumberjack/CocoaLumberjack.h>
In my case only one file was giving this error. Turned out that I added it to the project's tests target membership (in the File Inspector on the right).

Is there a way to suppress warnings in Xcode?

Is there a way to suppress warnings in Xcode?
For example I am calling an undocumented method and since the method is not in the header I get a warning on compile. I know I can add it to my header to stop the warning, but I am wondering if there is a way other than adding it to the header (so I can keep the headers clean and standard) to suppress the warning? A pragma or something?
To disable warnings on a per-file basis, using Xcode 3 and llvm-gcc-4.2 you can use:
#pragma GCC diagnostic ignored "-Wwarning-flag"
Where warning name is some gcc warning flag.
This overrides any warning flags on the command line. It doesn't work with all warnings though. Add -fdiagnostics-show-option to your CFLAGS and you can see which flag you can use to disable that warning.
there is a simpler way to suppress Unused variable warnings:
#pragma unused(varname)
EDIT:
source: http://www.cocoadev.com/index.pl?XCodePragmas
UPDATE:
I came accross with a new solution, a more robust one
Open the Project > Edit Active Target> Build tab.
Under User-Defined: find (or create if you don't find one )the key : GCC_WARN_UNUSED_VARIABLE set it to NO.
EDIT-2
Example:
BOOL ok = YES;
NSAssert1(ok, #"Failed to calculate the first day the month based on %#", self);
the compiler shows unused variable warning for ok.
Solution:
BOOL ok = YES;
#pragma unused(ok)
NSAssert1(ok, #"Failed to calculate the first day the month based on %#", self);
PS:
You can also set/reset other warning:
GCC_WARN_ABOUT_RETURN_TYPE : YES/NO
For gcc you can use
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow-ivar"
// your code
#pragma GCC diagnostic pop
You can learn about GCC pragma here and to get the warning code of a warning go to the Report Navigator (Command+9), select the topmost build, expand the log (the '=' button on the right), and scroll to the bottom and there your warning code is within square brackets like this [-Wshadow-ivar]
For clang you can use
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow-ivar"
// your code
#pragma clang diagnostic pop
In order to surpress a warning for an individual file do the following:
select the file in the xcode project.
press get info
go to the page with build options
enter -Wno- to negate a warning:
-Wno-
e.g.
-Wno-unused-parameter
You can get the name of the warning if you look on the project settings look at the GCC warnings located at the bottom of the build tab page, by clicking on each warning it will tell you the warning parameter name:
e.g.
Warn whenever a function parameter is
unused aside from its declaration.
[GCC_WARN_UNUSED_PARAMETER,
-Wunused-parameter]
With Objective-C, a number of serious errors only appear as warnings. Not only do I never disable warnings, I normally turn on "Treat warnings as errors" (-Werror).
Every type of warning in your code can be avoided by doing things correctly (normally by casting objects to the correct type) or by declaring prototypes when you need them.
To get rid of the warning: try creating a category interface for the object in question
#interface NSTheClass (MyUndocumentedMethodsForNSTheClass)
-(id)theUndocumentedMethod;
#end
...
#implementation myClass : mySuperclass
-(void) myMethod {
...
[theObject theUndocumentedMethod];
...
}
As an aside, I strongly advise against calling undocumented methods in shipping code. The interface can and will change, and it will be your fault.
http://nshipster.com/pragma/#inhibiting-warnings - skip to inhibiting warnings section
Create a new, separate header file called 'Undocumented.h' and add it to your project. Then create one interface block for each class you want to call undocumented functions on and give each a category of '(Undocumented)'. Then just include that one header file in your PCH. This way your original header files remain clean, there's only one other file to maintain, and you can comment out one line in your PCH to re-enable all the warnings again.
I also use this method for depreciated functions in 'Depreciated.h' with a category of '(Depreciated)'.
the best part is you can selectively enable/disable individual warnings by commenting or uncommenting the individual prototypes.
Suppressing that particular warning is not safe. The compiler needs to know the types of the arguments and returns to a method to generate correct code.
For example, if you're calling a method like this
[foo doSomethingWithFloat:1.0];
that takes a float, and there is no prototype visible, then the compiler will guess that the method takes a double, not a float. This can cause crashes and incorrectly interpreted values. In the example above, on a little endian machine like the intel machines, the receiver method would see 0 passed, not 1.
You can read why in the i386 ABI docs, or you can just fix your warnings. :-)