Would you be able to help to delete the warning discussed below ?
I have one file example.h
In that I declared static const NSString *variable = #"cool";
Now, I have another file multiple.m. In this file I have a function Bool var2 = [object methodname:variable];
It means, I passed variable as parameter for this function, but I got warning like this:
variable is defined but not used
So,how to remove this warning?
please help me
if example.h is imported in other files aside from multiple.m, then the compiler does the check in every file it is imported into, and reports when the variable is unused in that file.
if you want to get rid of the warnings, i would change that from a static const to a macro like so:
#define Variable #"cool"
GCC won't report on unused macros.
What you should probably be doing is:
extern NSString * const variable;
in the header and then
NSString * const variable = #"cool";
in an implementation (.m) file.
(Also note where the const is; you can see this pattern in Apple's own headers as, e.g., FOUNDATION_EXPORT NSString * const NSFileTypeSocket where FOUNDATION_EXPORT is a #define for extern.)
Related
I am trying to properly declare and define global variables in separate files and include them in a third file which deals with class declaration.
The three files are:
1) global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
extern const int marker_num;
extern const int dim;
using namespace std;
#endif // GLOBAL_H_INCLUDED
2) global.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
constexpr const int marker_num = 10;
constexpr const int dim = (2 * marker_num) + 3;
3) WorldState.h
#ifndef WORLDSTATE_H
#define WORLDSTATE_H
#include "global.h"
#include "global.cpp"
class WorldState{
public:
WorldState(float a[], float b[dim][dim]);
get_wstate();
protected:
private:
float w_state[];
float covar_matrix[dim][dim];
};
#endif // WORLDSTATE_H
I am using the global variable dim to declare and define a multidimensional array. I have declared dim inside global.h and defined it inside global.cpp. Now, I have a class called WorldState and inside its header, I am using dim. If I comment out #include "global.cpp", it throws the following error:
C:\Users\syamp\Documents\codeblocks\slam\WorldState.h|10|error: array bound is not an integer constant before ']' token
My understanding is that including the .h file includes the corresponding .cpp as well, and that all declarations should be inside .h and all definitions should be inside .cpp. However, it doesn't seem to work in this case.
1) If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice? I am trying to write a good code not just a code that works.
2) An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
3) I believe there is something that I am missing. Kindly suggest the best method to resolve this issue. I am using codeblocks and C++11. Thanks in advance.
I am using the global variable dim to declare and define a multidimensional array.
When declaring a fixed-length array at compile-time, the value(s) of its dimension(s) must be known to the compiler, but your separation prevents the value of dim from being known to the compiler at all, so dim cannot be used to specify fixed array dimensions. Any code that uses dim will just compile into a reference to it, and then the linker will resolve the references after compilation is done. Just because dim is declared as const does not make it suitable as a compile-time constant. To do that, you must define its value in its declaration, eg:
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
static constexpr const int marker_num = 10;
static constexpr const int dim = (2 * marker_num);
using namespace std;
#endif // GLOBAL_H_INCLUDED
Otherwise, if you keep dim's declaration and definition in separate files, you will have to dynamically allocate the array at run-time instead of statically at compile-time.
I have declared dim inside global.h and defined it inside global.cpp.
That is fine for values you don't need to use until run-time. That will not work for values you need to use at compile-time.
My understanding is that including the .h file includes the corresponding .cpp as well
That is not even remotely true. The project/makefile brings in the .cpp file when invoking the compiler. The .h file has nothing to do with that.
that all declarations should be inside .h and all definitions should be inside .cpp.
Typically yes, but not always.
If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice?
Yes.
An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
Yes, if you want to use them where compile-time constants are expected.
Is there anyway to do stringification in swift? , since there is no preprocessor it seems kind of difficult.
What I'd like to do is something similar to the following code.
// log a variable name and its value
#define LOGV(V) NSLog(#"%s = %#", #V, V);
NSString *myString = #"this";
LOGV(myString); // prints out -> "myString = ???"
After looking at dump it seems that there is no way of reflecting a variable name.
let myString = "this"
dump(myString, name:"myString", indent: 0 maxDepth: 0, maxItems: 1)
I also wanted to do the same thing in Swift,
so I recently wrote debug-logging util which stringifies variables at runtime instead (reading entire code!)
You might find this interesting.
https://github.com/inamiy/DebugLog
There is no way to do this in Swift. If you ABSOLUTELY want to do this, its always possible to run the C preprocessor over your Swift source.
I want to pass a dictionary around multiple methods, with a pre-defined key set. I've seen this done in classes I've used before, but am unsure how to set this up. This is what i'd use in the m file, for example:
NSString *name = [dictionary objectForKey:kObjectsName];
NSDate *date = [dictionary objectForKey:kObjectsDate];
How do i set up the pre-determined names for the dictionary keys?
Usually Apple leaves a bunch of constants defined in the header, like for example in the NSAttributedString Application Kit Additions:
Standard Attributes
Attributed strings support the following standard attributes for text. If the key is not in the dictionary, then use the default values described below.
NSString *NSFontAttributeName;
NSString *NSParagraphStyleAttributeName;
[...]
My suggest is to use your own constants if the attributes are way too many (with define or using global const variables).
For example in the .m file (where CN is the company name):
NSString* const CNURLKey= #"URLKey";
NSString* const CNupdateTimeKey= #"updateTimeKey";
NSString* const CNtagsKey= #"tagsKey";
NSString* const CNapplicationWillTerminateKey= #"applicationWillTerminateKey";
NSString* const CNtagAddedkey= #"tagAddedkey";
NSString* const CNtagRemovedKey= #"tagRemovedKey";
NSString* const CNcolorKey= #"colorKey";
And in the header file:
extern NSString* const CNURLKey;
extern NSString* const CNupdateTimeKey;
extern NSString* const CNtagsKey;
extern NSString* const CNapplicationWillTerminateKey;
extern NSString* const CNtagAddedkey;
extern NSString* const CNtagRemovedKey;
extern NSString* const CNcolorKey;
Or you may also use define as well.
You may also make things easier for the user, making a method that return a NSArray or NSSet containing the list of all variables.
If instead you need to hold just few attributes, reconsider the choice of using a dictionary, and use a class that contains all the attributes, accessible through KVC.
You can just put #define statements in your .m file:
#define kObjectsName #"myName"
#define kObjectsDate #"myDate"
etc.
Please help - this is driving me crazy...
I have a number of lines of code very similar but all of a sudden Xcode has started to think that some of the lines are errors and I can't build my app. Here is some extract code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
cycleSettingsDate = [defaults objectForKey:kCycleSinceDateKey];
cycleUnitFactor = [defaults boolForKey:kCycleUseMilesKey]? #"1.6" : #"1";
runUnitFactor = [defaults boolForKey:kRunUseMilesKey]? #"1.6" : #"1";
swimSettingsDate = [defaults objectForKey:kSwimSinceDateKey];
The item kCycleSinceDateKey is coloured pale blue as if it were a declared property in my header file like cycleSettingsDate - which it is not. The other three are coloured brown as you would expect a key to be colour-coded and they work perfectly. Only kCycleSinceDateKey does not work as expected.
I have the following in my header file which is an exact match with the plist identifier that it relates to:
#define kCycleSinceDate #"cyclesincedate"
#define kSwimSinceDateKey #"swimsincedate"
#define kRunUseMilesKey #"runusemiles"
#define kCycleUseMilesKey #"cycleusemiles"
I have exited Xcode and even restarted my mac for good measure but kCycleSinceDateKey is still underlined with the dotted red line and the same issue. Does anybody have any ideas?
Many thanks in advance...
In your code. you refer to it as kCycleSinceDateKey and in the define you refer to it as kCycleSinceDate.
Beyond the answers that pointed out the Key missing, I usually define my constants like this in .m. It's more compact. See When to use static string vs. #define
NSString * const kDefaultKeysCycleSinceDate = #"cyclesincedate";
and if I need to expose it elsewhere (usually why you define constants), do this in your header:
extern NSString* const kDefaultKeysCycleSinceDate;
Also:
Defining a globally accessible string in Objective-C
and #define vs const and linking against frameworks
I would also start each with the same prefix (kDefaultKeys...) so autocomplete shows you all the possible keys after you type a few chars ...
You declared a constant kCycleSinceDate and use kCycleSinceDateKey instead. Note that the declared constant name doesn't end with Key.
So either change de delcaration into:
#define kCycleSinceDateKey #"cyclesincedate" // Add Key
Or change the line where it is used into:
cycleSettingsDate = [defaults objectForKey:kCycleSinceDate]; // Remove Key
I have a C++ function which I call from Objective C.I need to pass variables by reference to the C++ function.But I get the following error in xcode - "Expected ';', ',' or ')' before '&' token in foo.h"
Function declaration in "foo.h"
#ifdef __cplusplus
extern "C"
{
#endif
NSString * LLtoUTM(double Lat,double Long,double &UTMNorthing, double &UTMEasting);
#ifdef __cplusplus
}
#endif
Function call in test_viewcontroller.m
double UTM_x;
double UTM_y;
UTMzone = [[NSString alloc] init];
UTMzone = (NSString *) LLtoUTM(latitude,longitude,UTM_y,UTM_x);
Can anyone tell me what is wrong?
Change the file to be test_viewcontroller.mm.
You told it to compile as an Objective-C file, which doesn't understand references. '.mm' means Objective-C++, which can mix the Obj-C and C++ together like what you're attempting to do.
You simply cannot do this in plain Objective-C — because references don't exist in C. They're a C++ feature. So you have to use Objective-C++, which basically means changing your Objective-C files' extensions to ".mm".