Learning Swift from scratch (Day 71) -- Data Type ing between Swift and C/C ++ Mixed Programming

Source: Internet
Author: User

Learning Swift from scratch (Day 71) -- Data Type ing between Swift and C/C ++ Mixed Programming
For Original Articles, please reprint them. Reprinted Please note: Guan Dongsheng's blog

If necessary header files are introduced, the C data type can be used in Objective-C. C Data type cannot be directly used in Swift. Apple provides the data type that corresponds to C language for Swift. These types mainly include basic data types and pointer types in C language.

Basic Data Types of C Language

As described in the table, the relationship between the Swift data type and the basic C language data type is shown.

The data types in the Swift language are the same as those in the native Swift language. They are essentially struct types. We can use their constructors to create instances of these data types. The sample code is as follows:

 

 

var intSwift = 80 //intvar intNumber  = NSNumber(int: CInt(intSwift)) //unsigned charvar unsignedCharNumber  = NSNumber(unsignedChar: CUnsignedChar(intSwift)) //unsigned intvar unsignedIntNumber  = NSNumber(unsignedInt: CUnsignedInt(intSwift))   

 

 

The variable intSwift stores 80 of the Int type. In the code, CInt (intSwift) is the instantiated CInt type, which converts the Int type to the C language int type and uses CInt in Swift.

In the code, CUnsignedChar (intSwift) converts the Int type to the C language unsigned char type and uses CUnsignedChar in Swift.

In the code, CUnsignedInt (intSwift) converts the Int type to the C language unsigned int type and uses CUnsignedInt in Swift.

C pointer type

As described in the table, the ing between Swift data type and C language pointer data type is described.

As can be seen from the table, Swift mainly provides three insecure generic pointer types: UnsafePointer. , UnsafeMutablePointer And AutoreleasingUnsafeMutablePointer . T is a wildcard placeholder, indicating different data types. In addition, the COpaquePointer type is the C pointer type that cannot be expressed in Swift.

Next we will introduce it separately.

1. UnsafePointer

UnsafePointer Is a commonly used constant pointer type, which requires the programmer to manually manage the memory, that is, to apply for and release the memory. It is generally created by other pointers. Its main constructor functions include:

  • Init (_ other: COpaquePointer ). Create a pointer using the COpaquePointer type.

  • Init (_ from: UnsafeMutablePointer ). You can use the UnsafeMutablePointer pointer to create an instance.

  • Init (_ from: UnsafePointer ). Create with UnsafePointer type pointer.

    UnsafePointer Main attributes:

    • Memory. Read-only attribute, which can access the content pointed to by the pointer.

      UnsafePointer Main Methods:

    • Successor ()-> UnsafePointer . Obtain the content of the next memory address pointed to by the pointer.

    • Predecessor ()-> UnsafePointer . Obtain the content of the previous memory address pointed to by the pointer.

      2. UnsafeMutablePointer

      UnsafeMutablePointer It is a common variable pointer type, which requires the programmer to manually manage the memory and apply for and release the memory. The variable pointer can be created by other pointers, or the variable pointer can apply for memory space through the alloc (_ :) method, and then call the initialize (_ :) method to initialize the pointer pointing to a value. When the pointer object is released, call the destroy () method to destroy the pointer pointing to the object. It is a reverse operation of the initialize (_ :) method. The two methods should appear in pairs in the code. Finally, call the dealloc (_ :) method to release the memory space pointed to by the pointer. It is a reverse operation of the alloc (_ :) method. These two methods should also appear in pairs in the code.

      3. AutoreleasingUnsafeMutablePointer

      AutoreleasingUnsafeMutablePointer It is called an automatic release pointer. It is declared as a parameter of this type in a method or function. It is of the input/output type. In the process of calling a method or function, the parameter is first copied to an unowned buffer and used in the method or function. When the method or function returns, the buffer data is rewritten back to the parameter.

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.