IOS 64-bit programming and ios64-bit Programming
1. Reject basic data types and implicit conversions 1) Basic Data Types
In 64-Bit mode, the basic type of long is changed from 4 bytes to 8 bytes. Take a look at the sizeof function in the program, and try not to use the basic data type, instead of the object type:
- Int-> NSInteger
- Unsigned-> NSUInteger
- Float-> CGFloat
- Animation time-> NSTimeInterval
- ...
2) implicit conversion
NSArray *items = @[<a href="http://www.jobbole.com/members/1/" rel="nofollow">@1</a>, @2, @3];for (int i = -1; i < items.count; i++) { NSLog(@"%d", i);}
The Return Value Type of array. count is NSUInteger. However, if I is-1,-1 is implicitly converted to a large positive integer when compared with I, resulting in a computing error. Be sure to pay attention to all operations related to this variable (assignment, comparison, conversion)
Change the basic data type to NSUInteger, and the initial value starts from 0. Here is only an example. It is more convenient to use for-in or block for traversal.
2. Use the new enumeration version
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { UIViewAnimationCurveEaseInOut, UIViewAnimationCurveEaseIn, UIViewAnimationCurveEaseOut, UIViewAnimationCurveLinear};
3. Replace the Format String
1) convert the unsigned long type to NSNumber:
(unsigned
long
) Change items. countNSNumber
Of@
Syntax sugar: @ (items. count ).
2) convert int type to string type:
NSInteger i = 10086;NSString *string = @(i).stringValue;
4. BOOL under 64-bit
In 32-bit, BOOL is definedsigned char
, @ Encode (BOOL) the result is'c'
In 64-bit mode, BOOL is definedbool
, @ Encode (BOOL) the result is'B'
5. The isa pointer is not directly obtained.
This is disabled by default by the compiler. The isa pointer is the Class address in 32 bits, but the actual address can be obtained using bits mask in 64 bits, use runtimeobject_getClass
Andobject_setClass
Method.
6. Solve third-party lib dependencies and lipo commands
Add targetarm64
For compiling, static library (. a) or framework, you need to find a new 64-bit package.
7. Will the package become larger after 64-bit support?
Yes.
Checklist
Note:
- Do not convert the pointer to an integer
- Uniform data types are used everywhere in the program
- Be sure to perform operations on Integers of different types.
- Use
int32_t, int64_t
This type of Fixed Length
- When using malloc, do not overwrite the size
- Use a formatted string that can adapt to both Architectures
- Pay attention to functions and function pointers (type conversion and variable parameters)
- Do not directly access the Objective-C pointer (isa)
- Use built-in synchronization Primitives (Primitives)
- Do not hard-code the size of the VM page
- Go Position Independent
References:
Http://ios.jobbole.com/81561/