Why use the Less parent selector (&) at the beginning of a line? - less

I understand what the parent selector does but I'm not sure I understand the syntax when it's at the beginning of a line. For example, this can be found in semantic-ui-less. For which use case would you wrap the import statement into & {}
/* Global */
& { #import "definitions/globals/reset"; }
& { #import "definitions/globals/site"; }
Why using this syntax instead of simply stacking the imports? What would the previous syntax do more over this one:
/* Global */
#import "definitions/globals/reset";
#import "definitions/globals/site";
Thanks!

Related

IntelliJ Velocity template $Character.isUpperCase return false on uppercase char

I'm trying to make a setter template which will allow me to use a m prefix for member variables. So when I have a field mTest is should give me a setter: public setTest and not setmTest. I think I have the correct logic, but Character.isUpperCase returns false even if it's a upper case letter. I've added some debugging a bit improvised, since it's kind of weird to test, because IntelliJ check if there is a proper function returned. When generating a setter I get an error dialog where I can see my output of:
#if($Character.isUpperCase($paramName.charAt(1)))
paramIsUppercase: $paramName.charAt(1)
#else
paramIsNotUppercase: $paramName.charAt(1)
#end
Complete code:
#set($paramName = $helper.getParamName($field, $project))
// debugging
#if($Character.isUpperCase($paramName.charAt(1)))
paramIsUppercase: $paramName.charAt(1)
#else
paramIsNotUppercase: $paramName.charAt(1)
#end
#if($StringUtil.startsWith($paramName, 'm') && $Character.isUpperCase($paramName.charAt(1)))
#set($paramName = $paramName.substring(1))
#end
#set($paramName = $StringUtil.decapitalize($paramName))
public ##
#if($field.modifierStatic)
static void ##
#else
$classname ##
#end
set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($paramName))($field.type $paramName) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName;
#if(!$field.modifierStatic)
return this;
#end
}
When using this to create a setter for mTest I get an error for my debugging
paramIsNotUppercase: T
Why is this returning false and is there a fix for this?
It looks like the problem is that $Character is not defined, which means the expression will always return false. There is a way to get around this. It is a horrible hack, but it works for me. Use the following template lines.
## get some object
#set($String='')
## abuse it to obtain the desired jdk class
#set($Character=$String.class.forName('java.lang.Character'))
After that you can use $Character regularly as you desire (i.e. $Character.isUpperCase($paramName.charAt(1))).
However there is no need to create your own setter template if you want to use prefixes for fields. Just go to the settings File | Settings | Editor | Code Style | Java | Code Generation and specify a Name prefix for Field and getters and setters will be generated correctly.

Importing a configuration file gives mach-o-linker

Its very strange, i have a .h file with many const and variables .
I have tWo classes that use him, hence import him:
classA , and classB with : ( #import "configurations.h" )
Now another new class that i have added, C , came in , and when i am trying to do the same with him, import the configuration file - i get a mach-o-linker error .
Check this out : even if the new class C is empty(!) i cant add that configuration file and get the same error . i have tried also with new classes , and its the same .
I could only add it then , to A B and now, no more importing !
EDIT
Ok, i now get it , first its not happen in class A and B because: A.m and B.mm hence i can import them both with that .h file .
Than the problem is probably because i dont use extern with my consts in the .h file ?
(i have tried extern and still got errors) , i create somehow 2 copies of consts from .h :
How i define them in .h file ? just like that :
const int wordSize=8;
const int numOfWords=8;
Whay am i missing here ?
(there is some other class D ,that import A B C if its matter )
.h file is for declarations
.m file is for definitions
It means no "=" signs in .h, no method implementations. If you have constant:
configurations.h
extern const int wordSize;
extern NSString * const someStringConst;
configurations.m
const int wordSize = 8;
NSString * const someStringConst = #"someStringConst";
If you import your
configurations.h
const int wordSize = 8;
from multiple files, compiler generates _wordSize symbol for every file that imported configurations.h
Linker for sure cannot resolve these duplicated symbols - so the best approach is to get rid of definitions in your .h file using extern declaration like i've shown above.

Generate random identifier in C Preprocessor to avoid duplicate linker symbols

I'm trying to solve Can Xcode tell me if I forget to include a category implementation in my target?, and I came up with the following solution:
NSObject+Foo.h
extern int volatile canary;
void canaryCage() {
canary = 0;
}
NSObject+Foo.m
int canary = 0;
Now, if I #import "NSObject+Foo.h" in a source file, I'll get a linker error if that NSObject+Foo.m wasn't also included in my target.
However, every time I #import "NSObject+Foo.h" I generate a duplicate _canaryCage symbol. I can't use __COUNTER__ because I only #import "NSObject+Foo.h" in implementation files. I need canaryCage to be unique across my whole symbol table.
I need something like:
#define CONCAT(x, y) x##y
#define CONCAT2(x, y) CONCAT(x, y)
extern int volatile canary;
void CONCAT2(canaryCage, __RANDOM__)() {
canary = 0;
}
This way, if I have source files like:
Bar.m
#import "NSObject+Foo.h"
Baz.m
#import "NSObject+Foo.h"
I'll get symbols like _canaryCage9572098740753234521 and _canaryCage549569815492345, which won't conflict. I also don't want to enable --allow-multiple-definition in ld because I want other duplicate symbol definitions to cause an error. I don't want to use canaryCage for anything but a marker that I forgot to link a source file whose header I #imported.
If you make it static, each translation unit will get its own copy, and everything else should work the way you want it to - no preprocessor gymnastics required.
static void canaryCage()
{
canary = 0;
}
This answer was close, but it resulted in canaryCage being optimized away because it was dead code.
Solution:
NSObject+Foo.h
extern int canary;
__attribute__((constructor)) static void canaryCage() {
canary = 0;
}
NSObject+Foo.m
int canary = 0;
Unfortunately, this adds some overhead every time the category is imported, but the overhead is very minimal. If anyone knows a way to prevent canaryCage from being stripped, I'll happily mark their answer as correct.

Objective C global constants with case/switch

Is there any way to use global int constants in Objective C that work in a case/switch statement? The technique here (http://stackoverflow.com/questions/538996/constants-in-objective-c) lets me access the constants everywhere, but does not let me put them into a switch statement.
in .h
FOUNDATION_EXPORT const int UNIT_IDLE;
FOUNDATION_EXPORT const int UNIT_DEFEND;
in .m
int const UNIT_IDLE = 0;
int const UNIT_DEFEND = 1;
Error is "Expression is not an integer constant expression"
I usually use enumerations with typedef statements when using constants which I will use in a switch statement.
For example, this would be in a shared .h file such as ProjectEnums.h:
enum my_custom_unit
{
MyCustomUnitIdle = 1,
MyCustomUnitDefend = 2
};
typedef enum my_custom_unit MyCustomUnit;
I can then use code similar to the following switch statement in my .c, .m, .cpp files:
#import "ProjectEnums.h"
- (void) useUnit:(MyCustomUnit)unit
{
switch(unit)
{
case MyCustomUnitIdle:
/* do something */
break;
case MyCustomUnitDefend:
/* do something else */
break;
default:
/* do some default thing for unknown unit */
break;
};
return;
};
This also allows the compiler to verify the data being passed to the method and used within the switch statement at compile time.
I think your best option is using enum types. Just declare a type in your header file and then you are ready to use it in a switch statement.
class.h
typedef enum{
kEditGameModeNewGame = 0,
kEditGameModeEdit = 1
}eEditGameMode;
class.m
eEditGameMode mode = kEditGameModeEdit;
switch (mode) {
case kEditGameModeEdit:
// ...
break;
case kEditGameModeNewGame:
// ...
break;
default:
break;
}
Good luck!
Official guideline says you should use "enumerations for groups of related constants that have integer values." That may solve your problem and improve code.

How do I use enumerated datatypes in Objective-C?

I'm working on several iOS projects where I think enumerated datatypes would be helpful to me. For example, I have a game where the player can walk in several directions. I could just define four constants with string values as kDirectionUp, kDirectionDown, etc.
I think an enumerated type would be better here. Is that correct? If so, how do I define an enum here so that I can later compare values? (As in, if(someValue == kDirectionUp){})
That sounds like the right thing to do.
It's really simple to create enums in Objective-C using C-style type definitions. For example, in one of my header files, I have the following type definition:
typedef enum {
CFPosterViewTypePoster = 0,
CFPosterViewTypeStart, // 1
CFPosterViewTypeEnd, // 2
.... // 3
} CFPosterViewType;
You define an object of CFPosterViewType and set it to one of the values:
CFPosterViewType posterType = CFPosterViewTypeStart;
When comparing CFPosterViewType values, it's as simple as doing the following:
if (posterType == CFPosterViewTypePoster) {
// do something
}
Note that the commented out numbers in the enum above are implicit values. If you want to do something differently, say, define a bitmask, or anything else where you need the values to be different than the default, you'll need to explicitly define them.
In a header file, define an enum type, e.g.:
// SomeHeaderFile.h
typedef enum {
MOPlayerDirectionNone,
MOPlayerDirectionUp,
MOPlayerDirectionDown,
…
} MOPlayerDirection;
Whenever you need to use MOPlayerDirection, #import that header file. You can then use it as a type as well as its possible values.
For instance:
#import "SomeHeaderFile.h"
#interface MOPlayer : NSObject {
MOPlayerDirection currentDirection;
}
- (void)moveToDirection:(MOPlayerDirection)direction;
- (void)halt;
#end
and:
#import "SomeHeaderFile.h"
#import "MOPlayer.h"
#implementation MOPlayer
- (id)init {
self = [super init];
if (self) {
currentDirection = MOPlayerDirectionNone;
}
return self;
}
- (void)moveToDirection:(MOPlayerDirection)direction {
currentDirection = direction;
switch (currentDirection) {
case MOPlayerDirectionUp:
// do something
break;
case MOPlayerDirectionDown:
// do something
break;
}
}
- (void)halt {
if (currentDirection != MOPlayerDirectionNone) {
// do something
currentDirection = MOPlayerDirectionNone;
}
}
#end
If an enumeration is tightly related to a class, it’s common to define it in the same header file as the class declaration. In the example above, instead of defining MOPlayerDirection in SomeHeaderFile.h, you could define it in MOPlayer.h instead.
Just define them at the top of your file:
enum // direction types
{
kDirectionUp = 0,
kDirectionDown, // 1
kDirectionLeft, // 2
kDirectionRight // 3
};
then you can call as required:
if(someValue == kDirectionUp){ // do something }