iOS How to detect iPhone X, iPhone 6 plus, iPhone 6, iPhone 5, iPhone 4 by macro?

Swift import UIKit public enum DisplayType { case unknown case iphone4 case iphone5 case iphone6 case iphone6plus static let iphone7 = iphone6 static let iphone7plus = iphone6plus case iphoneX } public final class Display { class var width:CGFloat { return UIScreen.main.bounds.size.width } class var height:CGFloat { return UIScreen.main.bounds.size.height } class var maxLength:CGFloat { return max(width, … Read more

How can I convert from degrees to radians?

Xcode 11 • Swift 5.1 or later extension BinaryInteger { var degreesToRadians: CGFloat { CGFloat(self) * .pi / 180 } } extension FloatingPoint { var degreesToRadians: Self { self * .pi / 180 } var radiansToDegrees: Self { self * 180 / .pi } } Playground 45.degreesToRadians // 0.7853981633974483 Int(45).degreesToRadians // 0.7853981633974483 Int8(45).degreesToRadians // 0.7853981633974483 … Read more

Macro definition to determine big endian or little endian machine?

Code supporting arbitrary byte orders, ready to be put into a file called order32.h: #ifndef ORDER32_H #define ORDER32_H #include <limits.h> #include <stdint.h> #if CHAR_BIT != 8 #error “unsupported char size” #endif enum { O32_LITTLE_ENDIAN = 0x03020100ul, O32_BIG_ENDIAN = 0x00010203ul, O32_PDP_ENDIAN = 0x01000302ul, /* DEC PDP-11 (aka ENDIAN_LITTLE_WORD) */ O32_HONEYWELL_ENDIAN = 0x02030001ul /* Honeywell 316 (aka … Read more

Creating C macro with ## and __LINE__ (token concatenation with positioning macro)

The problem is that when you have a macro replacement, the preprocessor will only expand the macros recursively if neither the stringizing operator # nor the token-pasting operator ## are applied to it. So, you have to use some extra layers of indirection, you can use the token-pasting operator with a recursively expanded argument: #define … Read more

Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?

Apple included full support for Swift preprocessor flags as of Xcode 8, so it’s no longer necessary to set these values in “Other Swift Flags”. The new setting is called “Active Compilation Conditions”, which provides top-level support for the Swift equivalent of preprocessor flags. You use it in exactly the same way as you would … Read more

Optional Parameters with C++ Macros

Here’s one way to do it. It uses the list of arguments twice, first to form the name of the helper macro, and then to pass the arguments to that helper macro. It uses a standard trick to count the number of arguments to a macro. enum { plain = 0, bold = 1, italic … Read more

Comma in C/C++ macro

If you can’t use parentheses and you don’t like Mike’s SINGLE_ARG solution, just define a COMMA: #define COMMA , FOO(std::map<int COMMA int>, map_var); This also helps if you want to stringify some of the macro arguments, as in #include <cstdio> #include <map> #include <typeinfo> #define STRV(…) #__VA_ARGS__ #define COMMA , #define FOO(type, bar) bar(STRV(type) \ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)