64-bit Tips

Source: Internet
Author: User
Tags uikit

After all, still come. Apple has issued an ultimatum to support the 64-bit:

As we announced in October, beginning February 1, new IOS apps submitted to the APP Store must include 64-bit support and is built with the IOS 8 SDK. Beginning June 1, app updates would also need to follow the same requirements.

The adaptation that should have been done has finally begun, and the 64-bit CPU has run a 32-bit program for so long. The previous period of time the company project completed the 64-bit package, this is not so complicated by the numerous non-standard old code stirring not light, summed up a few tip mutual encouragement.

Tips reject basic data types and implicit conversions

The first is the basic type, such as the following 4 types in the 32-bit and 64-bit, respectively, how long?

1234
sizeof (sizeof (sizeof (double);   

32-bit under: 4, 4, 4, 8 64-bit:4, 8, 4, 8
(PS: This result with the compiler, for other platforms may not necessarily)
Their length changes may not be our expectation of doubling the length of the 64-bit, so the code that appears in sizeof the program looks at the two more eyes. And, unless you know exactly what you're doing, you should use the following type instead of the base type:

    • Nsinteger int
    • Unsigned-Nsuinteger
    • Float-CGFloat
    • Animation Time-Nstimeinterval
    • ...

These are the types defined in the SDK, and most of the time we are dealing with the SDK APIs, which can be used to reduce the impact of type conversions a lot.

Let's say the following code:

1234
@[@3];  for (int i =-1; i < Items.Count; i++) {NSLog (@ "%d", i);}      

As a result, the For loop does not go in at once.
The array count is of NSUInteger type, and 1 is implicitly converted NSUInteger to a large number when compared to:

1234
(LLDB) P I ($=-18446744073709551615 

This has nothing to do with 64-bit, to illustrate, this implicit conversion also needs to be careful, be aware of all the operations associated with this variable (assignment, comparison, conversion)
The old-fashioned for loop can be considered written as:

1
Index < items.  index++) {}

Of course, array traversal is more recommended for-in or block version, and the comparison between them can be reviewed in this article.

Using the new enumeration

Similar to the above, enumerations should use the new version of the notation:

123456
typedef ns_enum (Nsinteger, uiviewanimationcurve) {    uiviewanimationcurveeaseinout,    Uiviewanimationcurveeasein,    uiviewanimationcurveeaseout,    uiviewanimationcurvelinear};

Not only can you specify a type for the enumeration value, but the compiler will also give a warning when assigning the wrong type, and there is no reason to use this notation.

Replace format string

When adapting to the 64-bit, did you encounter the following disgusting notation:

12
@[@3]; NSLog (@ "number of array elements:%lu", (unsigned long) items.count);   

In general, the use NSNumber of the @ syntax sugar can be solved:

12
@[@3]; NSLog (@ "number of array elements:%@", @ (Items.Count));   

Similarly, int to string can also:

12
10086; NSString *string = @ (i). stringvalue; 

Of course, %.2f This format does not apply if needed.

BOOL under the 64-bit

32-bit, BOOL is defined as the signed char result of @encode (BOOL) is‘c‘
64-bit, BOOL is defined as bool the @encode (bool) result is‘B‘
The more intuitive explanation is:

1234
(LLDB) p/t (Char)7 (bool) $0b00000111 (YES) (LLDB) p/t (bool)7 (bool) $  0b00000001 (YES)         

The 32-bit version of BOOL includes the possibility of 256 values and also causes some pits, as this article says. and 64-bit only 0 (NO), 1 (YES) two possible, finally to bool name.

Do not take the ISA pointer directly

The compiler has disabled this use by default, the ISA pointer is the address of class under 32 bits, but using bits mask under 64 bits to remove the real address, if you really need to, use runtime object_getClass and object_setClass methods. For a 64-bit explanation of ISA, you can read this article

Resolve third-party Lib dependencies and Lipo commands

In the form of the source code in the project of the third-party LIB, as long as the target plus arm64 compile the good.
The disgusting thing is that the static libraries (. A) or the framework, which are dragged directly into the project, need to be re-searched for the package that supports 64-bit. This time can see which is already unmanned maintenance of LIB, it is a substitute for a replacement (for example, I can not find the whole network of the project used in an audio library of the 64-bit package, finally found on a buddy's GitHub, crying to give a star--)

Print supported schemas for mach-o files

How to see if an executable file supports 64-bit?

Use lipo -info commands, such as looking at Uikit supported schemas:

123
File:uikit.framework/uikit are:arm64 armv7s

To see more detailed information can be used lipo -detailed_info :

 12345678910111213141516 
 sunnyxx$ Li Po-detailed_info uikit.framework/uikit Fat header in:uikit.framework/uikitfat_magic 0xcafebabenfat_arch 2architecture arm64 cputype cpu_type_arm64 cpusubtype CPU_SU Btype_arm64_all offset 4096 size 16822272 align 2^12 (4096) architecture armv7s Cputype cpu_type_arm cpusubtype cpu_subtype_arm_v7s offset 16826368 size Span class= "number" >14499840 align 2^12 (4096)             

Of course, you can also use file commands:

1234
2 Architecturesuikit.framework/uikit (64-libraryuikit.framework/uikit (library arm  

The above commands Mach-O apply to files, static libraries .a , files in the framework .a , and executable files of their own apps can all be printed under a look.

Merging packages with multiple schemas

If we have MyLib-32.a and MyLib-64.a , you can use the lipo -create command merge:

1
sunnyxx$ Lipo-Create mylib-.  A mylib-.  A-output Mylib. a     
Will the package become larger after 64-bit support?

Will, support 64-bit, a more arm64 architecture, in theory, each architecture a set of instructions, but compared to the original will be much more difficult to say, we have increased by about 50%, and heard that will increase by one times.

A lib contains a lot of architecture, will it hit the last bag?

No, if there is a schema in Lib, armv7, armv7s, arm64, i386 and target architecture chooses armv7s, arm64 , then only the binary code of the two schemas specified by link from Lib will not link to the final executable in the other schemas; A lib requires a normal link in the emulator environment and also contains instructions for the i386 architecture.

Checklist

Finally, make a list of the points of note in the official documentation:

    • Do not turn the pointer strongly into an integer
    • Unified data types are used throughout the program
    • It is important to pay attention to the operation of different types of integers.
    • When you need a fixed-length variable, use a int32_t, int64_t fixed-length type like this
    • Do not write dead size when using malloc
    • Use a formatted string that can fit two schemas at the same time
    • Note Function and function pointers (type conversions and mutable parameters)
    • Do not directly access the OBJECTIVE-C pointer (ISA)
    • Using the built-in synchronization primitives (primitives)
    • Do not hardcode the virtual memory page size
    • Go Position Independent
References

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ Introduction/introduction.html#//apple_ref/doc/uid/tp40013501-ch1-sw1
Http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
http://www.bignerdranch.com/blog/64-bit-smorgasbord/
http://www.bignerdranch.com/blog/bools-sharp-corners/

64-bit Tips

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.