How to make 3 bool global variable? - ios

I'm creating a word game and I need to assign coins to a user:
firstly I've read over and over on subjects "Global variables" Unfortunately i'm not able to successfully do them.
I need to make 3 bool global variable such as btn1Pressed,btn2Pressed,btn3Pressed.
& how to make the word "coins" global of type int as well?
How is this done?
#interface MainView :

To declare global variables, you need to declare them outside of any class definition. If I have a lot of global variables, I usually put them in their own source file and call it globals.m to help keep things organized. If I only have one or two, I just declare them in the App delegate (after the #includes but before the #implementation).
int coins;
BOOL btn1Pressed;
BOOL btn2Pressed;
BOOL btn3Pressed;
// etc
Then you declare them in a common header (I use globals.h) as extern. Declare them outside of any class definition:
extern int coins;
extern BOOL btn1Pressed;
extern BOOL btn2Pressed;
extern BOOL btn3Pressed;
...
That tells the compiler that "this variable is declared elsewhere", so it knows to resolve it at link time. Include that header in any source module that needs access to those variables.

Using the keyword "extern" you can make variables global.
extern int coins = 9001; //over nine thousand!

If you mean "globals" as in "accessible everywhere in your app", you can use singletons:
http://www.galloway.me.uk/tutorials/singleton-classes/
This way you can group all such global variables in one global object.

Using Singleton pattern is the ideal way to do it.
Create your properties in YourAppdelegate.h file for your bools btn1Pressed,btn2Pressed,btn3Pressed.Further synthesize them in YourAppdelegate.m file.
Now if you want to access or change these values in any other classes
// Accessing btn1Value
BOOL checkTheValueOFButton1 = [(YourAppDelegate *)[UIApplication sharedApplication]btn1Pressed];
If you want to change the value
[(YourAppDelegate *)[UIApplication sharedApplication]setbtn1Pressed:NO];

Related

Why we don't need to provide initial value for local variables?

While I was learning suddenly I wondered myself:
why do we have to provide initial values for global(even beyond a class scope) variable but we do not have to do same step with local variables like this? Is there any reason?
if importRequired {
let deleteObjectCount: Int
}
It is allowed, because deleteObjectCount is never been used in your code. And - and this is the difference to global variables - this fact can be checked by the compiler.
You could even do something like:
let importRequired = true
if importRequired {
let deleteObjectCount: Int
deleteObjectCount = 5
print (deleteObjectCount)
}
(e.g. kind-of modify a constant let variable) because the compiler checks that the constant is written only once, and this is done before reading it's value.
In contrast, global variables must be initialized directly, because otherwise the compiler cannot guarantee that they have been so before being initialized (because the could be accessed from anywhere in your program).

Type name does not allow storage class to be specified?

Here is code in log.h file :
struct SUCC_CODE{
static const int RECOGNIZER_OK = 0;
};
The above piece of code in log.h file throwing compiler error:
Type name does not allow storage class to be specified
Struct members may not be static. Remove that specifier, and the compiler should stop complaining. This question explains that it is a valid specifier in C++.
C doesn’t allow you to use static within a struct. It’s not even clear what that would mean in a C struct.

in Dart, problems when attempting to "register" sub-class with super-class

I wish to have the sub-classes of a super-class "registered" by an arbitrary name - whenever I declare a sub-class I wish to also have it entered into the super-class.sub Map.
Is there any way to accomplish this outside of main()?
// base class
class Mineral{
final String formula;
static Map<String,Mineral> sub = {}
Mineral( this.formula );
}
// sub class - declare and register
class Mica extends Mineral{
Mica( String formula ) : super( formula );
}
Mineral.sub['mica'] = Mica; // oops!
when I run this, I get
Error: line 10 pos 1: unexpected token 'Mineral' Mineral.sub['mica'] = Mica;
assuming that executable code is not allowed outside main().
cannot put within the super-class since other sub-classes may declared later, outside the library.
Dart has no way to run code as part of a library being loaded.
Executable code can only be put inside methods, or in field initializers, and static field initializers are lazy so they won't execute any code until you try to read them.
This is done to ensure quick startup - a Dart program doesn't have to execute any code before starting the main library's "main" method.
So, no, there is no way to initialize something that isn't constant before main is called.
Either
Mineral.sub['mica'] = new Mica();
or
static Map<String,Type> sub = {};
When you assign Mica you assign the Type Mica. new Mica() is an instance of Mica that is of the kind Mineral and can be assigned to the map you declared.
edit
Maybe you want to initialize the sub map:
static Map<String,Mineral> sub = {'mica': new Mica()};
hint: the semicolon is missing in this line in your question.

iOS blocks, how to use in different implementation files

I am trying to make some reusable blocks for my application.
CommonBlocks.h
void (^testBlock)(int) = ^(int number) {
// do nothing for now;
};
VariousImplementationFile.m
#import "CommonBlocks.h"
(void)setup {
testBlock(5);
}
Unfortunately, when I try to push this code to iOS device I receive error: linker command failed with exit code 1 (use -v to see invocation). It seems that I missing some.
Any advice?
Thanks
You try add static keyword before the declaration:
static void (^testBlock)(int) = ^(int number) {
// do nothing for now;
};
Your code causes error because you have non-static variable testBlock declared in .h header file.
When you call #import "CommonBlocks.h" in VariousImplementationFile.m, testBlock is declared once. Then you import CommonBlocks.h in some where else, testBlock is declared once more, so you'll get symbol duplicate error.
Declare block in CommonBlocks.h this way
typedef void (^RCCompleteBlockWithResult) (BOOL result, NSError *error);
Then you may use in any method for example:
-(void)getConversationFromServer:(NSInteger)placeId completionBlock:(RCCompleteBlockWithResult)completionBlock
This is not specific to blocks. Basically, you want to know how to have a global variable that is accessible from multiple files.
Basically, the issue is that in in C, each "symbol" can only be "defined" once (it can be "declared" multiple times, but just be "defined" once). Thus, you cannot put the "definition" of a symbol in a header file, because it will be included in multiple source files, so effectively, the same symbol will be "defined" multiple times.
For a function, the prototype is declaration, and the implementation with the code is the definition. You cannot implement a function in a header file for this reason. For a regular variable, writing the name and type of the variable is defining it. To only "declare" it, you need to use extern.
It is also worth mentioning static. static makes a variable local to a particular source file. That way, its name won't interfere with variables with the same name elsewhere. You can use this to make global variables that are "private" to a particular file. However, that is not what you are asking for -- you are asking for the exact opposite -- a variable that is "public", i.e. shared among files.
The standard way to do it is this:
CommonBlocks.h
extern void (^testBlock)(int); // any file can include the declaration
CommonBlocks.m
// but it's only defined in one source file
void (^testBlock)(int) = ^(int number) {
// do nothing for now;
};

global variable from Project1.cpp accesible in Unit1.cpp

How to declare a global variable in Project1.cpp and read it in Unit1.cpp ?
This question is about C++ Builder
Use extern keyword in Unit1.cpp to refer the declaration from Project1.cpp.
For example, if in Project1.cpp you have
// Global variable
int myGlobalVar;
then in Unit1.cpp you should have
extern int myGlobalVar;
However, this practice is questionable and should be avoided. Important programming principles like modularization and decoupling can be denied by the usage of the global variables.
Isn't it better to do something like this?
In Project1.h, declare a variable in the public area.
public // User declarations
__fastcall TForm1(TComponent* Owner);
double MyVar;
Then include Project1.h in Unit1.cpp and then MyVar can be accessed as
Form1->MyVar

Resources