how to get constant #define value in swift class.
I have created constant.h class in my project, here I created the screen width and hight two constants values.
Constant.h
#define SCREEN_WIDTH_SWIFT UIScreen.main.bounds.size.width
#define SCREEN_HEIGHT_SWIFT UIScreen.main.bounds.size.height
Now i want to asses SCREEN_HEIGHT_SWIFT in ViewController.swift class
NSFontAttributeName : UIFont(name: "GillSans-Light" , size: MS_SCREEN_HEIGHT_SWIFT/40.5)!,NSForegroundColorAttributeName : UIColor.white
#define creates a C style compiler macro, not a constant. Swift doesn't support C compiler macros. You will need to use an actual constant.
Hi instead of using #define use let and assign the values with a =:
let SCREEN_WIDTH_SWIFT = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT_SWIFT = UIScreen.main.bounds.size.height
Simply create separate class to maintain your all constants.
Lets say AppConstats is your class. Then create your constants like this:
static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
Now wherever you want to access your constant, simply use like this:
AppConstats.SCREEN_WIDTH
Related
I want to create constants in header file of type CGSize so I can use this size anywhere in my app just using constantName.height and constantName.Width.
I would appreciate if you provide syntax for both cases: 1) fixed height and width, 2) height and width as passing value
Thank you
For fixed height and width
#define MAXSIZE CGSizeMake(320, 480)
For passing values, you can give the value MySizeType i.e defined as CGSize. But for Constant why do you want to pass values.
typedef CGSize MySizeType;
EDIT
After few comments not to use Macros I am elaborating my answer over here.
Using MACROS the drawback is that your debugger cannot know the constant.
And also there more ways to create a constant depends on the scope of your constant you want,
For internal class only
static CGSize const MAXSIZE = {320, 480};
For outside class
In .h file
extern CGSize const MAXSIZE;
In .m file
CGSize const MAXSIZE = {320,480};
Here is your answer.
1) Fixed
#define kSize CGSizeMake(13.0f, 34.0f)
2) Pass value
#define kSize(width, height) CGSizeMake(width, height)
You can use like this
CGSize size = kSize(12.0f,12.0f);
NSLog(#"%#",NSStringFromCGSize(size));
I have used typedef NS_ENUM to reorganise data constants in old code. Using an approach found here every typedef is declared in a single .h file that can be imported to any class in the project. Content of the .h file is wrapped in a message to the compiler. This works nicely for int variables.
MYCharacterType.h
#ifndef MYCharacterType_h
#define MYCharacterType_h
typedef NS_ENUM(NSInteger, MARGIN)
{
MARGIN_Top = 10,
MARGIN_Side = 10,
MARGIN_PanelBaseLine = 1
};
...
#endif /* SatGamEnumType_h */
But Xcode complains when I try to include float variables
“Non-Integral type ’NSNumber’ is an invalid underlying type’
e.g.
typedef NS_ENUM(NSNumber, LINE_WIDTH) {
LINE_WIDTH_Large = 1.5,
LINE_WIDTH_Medium = 1.0,
LINE_WIDTH_Small = 0.5,
LINE_WIDTH_Hairline = 0.25
};
I get the same message whether I use NSValue or NSNumber so I suspect typedef NS_ENUM is not the way to define float variables (or at least the way I am using it).
The approach in this answer would only allow me to do what I have already organised in one file but does not offer a way to reorganise float variables in the same file. Could someone please explain how to do this so all variables are defined in one .h file regardless of their type ? Thanks
SOLUTION
This was answered by rmaddy after I approached the question differently.
Defining different enums in one .h .. like just add it one file.
typedef NS_ENUM(NSInteger, MARGIN)
{
MARGIN_Top = 10,
MARGIN_Side = 10,
MARGIN_PanelBaseLine = 1
};
typedef NS_ENUM(long, ENUM_2)
{
ENUM_2_1 = 10,
ENUM_2_2 = 20,
ENUM_2_3 = 30,
};
typedef NS_ENUM(long, ENUM_3)
{
ENUM_3_1 = 10,
ENUM_3_2 = 20,
ENUM_3_3 = 30,
};
// And so on as many as you want
And your second question, Enums can only be of the integral data types like, int, long, long long, unsigned int, short etc... You can't use any Non-Integral types like float or double or not even any objective c types.
You can do enum mapping for float values like this https://stackoverflow.com/a/8867169/1825618
How to create NSString of zero width space.
In Objective-C, I used to create like this:
NSString *emptyText = #"\u200B";
How to create it in Swift? I have tried following, but it is not working.
let emptyString = "\u200B"
Use this syntax in Swift
let emptyString = "\u{200B}"
Of course you can also use unicode characters directly in your Swift code.
I'd like to create a NSObject subclass which would hold all my values for UIView.
Problem - what's the right way to do that?
Using "extern" and class method combination?
Using "extern" and #define combination?
Using only #define on class methods?
UI elements(e.g. UIColor) can't be initialized using "extern *const" method.
Writing a class methods for each value seems like too much.
Macros are plain(no coloring, etc.) and are declared only in header file.
Isn't there are better solution, which would hold all my ints,floats, colors in same place. And which is not a macro.
Why not macros?
For UIColor you can use:
#define RGBA_COLOR(r, g, b, a) [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:a]
#define MY_GREEN_COLOR RGBA_COLOR(60, 192, 174, 1.0)
And then you can use MY_GREEN_COLOR without problems:
UIColor *color = MY_GREEN_COLOR;
And the same for int, floats and so on:
#define MY_INT 83
I usually have a "Globals.h" file with all this stuff.
I have different size of pages. I want to use enum to select size of page. somethink like that
typedef NS_ENUM(CGSize, SizeType) {
MAXSIZE=CGSizeMake(640, 1196),
MIDIUMSIZE=CGSizeMake(320, 590),
MINSIZE=CGSizeMake(160, 280)
};
its possible? if not then whats the best way to do this i need this combine value in my whole application
An enum in C (and therefore in Objective-C) is a set of integer values, and that's why you cannot have CGSize values as members of it.
Instead, use constants. The best option is to look at what Apple does and mimic it.
If you take a look at CGGeometry.h you will find the definitions of various constants.
For instance, CGSizeZero is defined as
CG_EXTERN const CGSize CGSizeZero
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
You can then do something similar by declaring a constant in your header
CG_EXTERN const CGSize kMaxSize;
and then defining it in the implementation
const CGSize kMaxSize = (CGSize){ 640, 1196 };
As a bonus you can also define a type synonym for CGSize, for instance:
typedef CGSize MySizeType;
and then use it for declaring both constants and variables, e.g.
CG_EXTERN const MySizeType kMaxSize;
...
#property (nonatomic) MySizeType aSize;
That does't change a bit from a technical point of view, but it's semantically nicer and it basically achieves the same purpose of a typedef enum (which is precisely providing a convenient synonym to int)
As per the other answers, enums are basically integers, not structs.
You can just #define the values in a constants file:
#define MAXSIZE CGSizeMake(640, 1196)
#define MIDIUMSIZE CGSizeMake(320, 590)
#define MINSIZE CGSizeMake(160, 280)
though you might want to rename them for easier mnemonics, readability and auto-completion purposes, like:
#define PURPOSE_SIZE_MAX ...
#define PURPOSE_SIZE_MED ...
...
You cannot.
The enum type is a C type and it must be integer types, each member must be the same type also.
You can use char, BOOL, int, uint, NSInteger and so on.
For constant floating point values, you will need to declare them one by one.
structs also need to be done one by one.
You can not use enum for this. In Objective C enum is inherited from C. So it is implicitly converted to int.