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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.