# Define collection commonly used in iOS,

Source: Internet
Author: User

# Define collection commonly used in iOS,

1. Define Constants

When defining constants, it is best to start with a lower-case letter k to give people an insight,

(1) Navigation Bar Height: We all know that the height of the navigation bar is 44 when the iPhone is portrait screen. At this time, a constant can be defined to indicate the height,

#define kNaivgationBarHeight 44

(2) screen width and height: the screen width and height are the screen size of the iOS device hardware, which is different from the view of ViewController,

#define kSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define kSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

2. safe release objects for Memory Management

#define SAFE_RELEASE(x) [x release];x=nil

Note that there is no end; colon, which is used in dealloc, for example

- (void)dealloc{    SAFE_RELEASE(array);    [super dealloc];}

Why does this statement indicate secure release? When using an Objective-C object, we must ensure that its reference count retainCount is 0, but sometimes we cannot guarantee perfection, at this time, when dealloc is used, the object is set to nil, which releases the memory area of the comrade-in-arms of the object to prevent memory leakage.

3. Determine the version of the iOS system

(1) Current System Version Number

#define kCurrentSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]

(2) Determine whether the system version is iOS 7 or later.

#define IOS_VERSION_7_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0)? (YES):(NO))

(3) Current System Language

#define kCurrentLanguage [[NSLocale preferredLanguages] objectAtIndex:0]

4. define common colors

Sometimes multiple controls need to set the same color, while the rgb format of UIColor is indeed a waste of time. Macro-defined constants can save a lot of code. For example, purple and dark gray are defined below,

#define kPurpleColor [UIColor colorWithRed:137.0/255 green:21.0/255 blue:89.0/255alpha:1.0]#define kDarkGrayColor [UIColor colorWithRed:100.0/255 green:100.0/255 blue:100.0/255alpha:1.0]

At this time, it is much easier to define the background color for the control.

5. Define dlogs that are more advanced than NSLog

NSLog facilitates brute-force debugging, that is, to output the value of the variable we observe. It can encapsulate NSLog more advanced by macro definition. The following code is used in the pch file of the project,

#define DEBUG_MODE 1#if DEBUG_MODE#define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )#else#define DLog( s, ... )#endif

For example, use DLog (@ "12345") in ViewController; the output content on the console is as follows,

2014-04-18 19:33:30.377 DefineSample[3593:70b] <0x8a68360 ViewController.m:(54)> 12345

This section contains the memory address of the string @ "12345" <0x8a68360>, 54 lines of the ViewController. m file, and the string content is 12345. In fact, let's take a look at the definition of these macros. We can understand more system things, such as _ FILE _, which indicates the FILE to be located and _ LINE _ specifies the row to be located.

6. Determine whether it is an iPhone Device or a Simulator)

# If TARGET_ OS _IPHONE // encode NSLog (@ "iPhone Device") for the real machine; # elif TARGET_IPHONE_SIMULATOR // encode NSLog for the Simulator (@ "iPhone Simulator"); # endif

Sometimes the performance of the simulator is different from that of the real machine, so you can make a judgment. The above macros TARGET_ OS _IPHONE and TARGET_IPHONE_SIMULATOR are defined by the system and can be used directly. Press and hold the Command button to view more information.

7. Determine if it is ARC

// ARC # if _ has_feature (objc_arc) // uses arc encoding # else // uses manual memory management # endif

8. Define the background thread and main thread of GCD

// Run in the background # define BACK_GCD (block) dispatch_async (dispatch_get_global_queue (queue, 0), block) // run in the main thread # define MAIN_GCD (block) dispatch_async (Queue (), block)

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.