original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
if the necessary header files are introduced, theobjective-clanguage can be used inCThe data type. And inSwiftcannot be used directly in a languageCdata type, Apple isSwiftLanguage Provision andCThe language corresponds to the data type. These types mainly include:Clanguage basic data types and pointer types.
C Language basic data Types
as described in the table is Swift data type and C The language base data type corresponds to the relational table.
650) this.width=650; "Width=" "height=" 368 "title=" 70-1.jpg "style=" width:600px;height:368px; "alt=" Wkiol1bfv-sbyvwcaahcwkzmzu8827.jpg "src=" http://s1.51cto.com/wyfs02/M00/7D/12/ Wkiol1bfv-sbyvwcaahcwkzmzu8827.jpg "border=" 0 "vspace=" 0 "hspace=" 0 "/>
Swift these data types in the language are associated with Swift native data types are essentially struct types. We can create instances of these data types with their constructors. The sample code is as follows:
var intswift =//intvar intnumber = NSNumber (Int:cint (intswift))//unsigned charvarunsignedcharnumber =NSNumber (unsi Gnedchar:cunsignedchar (intswift))//unsigned Intvar unsignedintnumber = NSNumber (Unsignedint:cunsignedint (IntSwift) )
variablesIntswiftthe stored theis aInttype. In your codeCInt (Intswift)is an instantiation ofCInttype, which implements theInttype intoClanguageinttype, inSwiftused inCIntrepresentation.
code in cunsignedchar (intswift) int type converted to c unsigned char swift cunsignedchar > said.
in the CodeCunsignedint (intswift))is toInttype intoClanguageunsigned inttype, inSwiftused inCunsignedintrepresentation.
C language pointer type
as described in the table is Swift data type and C The language pointer data type corresponds to the relational table.
650) this.width=650; "Width=" "height=" 188 "title=" 71-2.jpg "style=" width:600px;height:188px; "alt=" Wkiom1bfv63b8wkoaahx8qgvohm208.jpg "src=" http://s5.51cto.com/wyfs02/M01/7D/14/ Wkiom1bfv63b8wkoaahx8qgvohm208.jpg "border=" 0 "vspace=" 0 "hspace=" 0 "/>
visible from table forCthe language of a variety of pointer forms,Swiftmainly by providing three kinds of unsafe generic pointer types:unsafepointer<t>,unsafemutablepointer<t>and theautoreleasingunsafemutablepointer<t>. Tis a generic placeholder that represents a different data type. In addition, there areCopaquepointertype isSwiftcannot be represented in theCpointer type.
let's introduce the following separately.
1.unsafepointer<t>
unsafepointer<t> is a more commonly used constant pointer type, a pointer object that requires the programmer to manually manage the memory itself, that is, to request and free the memory itself. It is generally created by other pointers. Its main constructors are:
Init (_ Other:copaquepointer). Created by the Copaquepointer type pointer.
Init<u> (_ from:unsafemutablepointer<u>). Created by the Unsafemutablepointer type pointer.
Init<u> (_ from:unsafepointer<u>). Created by the Unsafepointer type pointer.
Unsafepointer<t> The main properties:
Memory. A read-only property that accesses the content that the pointer points to.
Unsafepointer<t> The main methods:
Successor (), Unsafepointer<t>. Gets the contents of the next memory address pointed to by the pointer.
Predecessor (), Unsafepointer<t>. Gets the contents of the previous memory address pointed to by the pointer.
2. unsafemutablepointer<t>
UNSAFEMUTABLEPOINTER<T> alloc (_:) method request memory space, and then call initialize (_:) method initializes the pointer to a value. When the pointer object is released, you need to call destroy () method destroy Pointer to object , it is initialize (_:) method reverse operation, Their two methods should appear in pairs in the code. Finally call dealloc (_:) alloc (_:) The reverse operation of the method, These two methods should also appear in the code.
3. autoreleasingunsafemutablepointer<t>
autoreleasingunsafemutablepointer<t> called an auto-release pointer, declared in a method or function as a parameter of that type, is an input-output type, in the process of invoking a method or function, the parameter is first copied to a non-ownership buffer, the buffer used within the method or function, and when the method or function returns, the buffer data is re-written back to the parameter.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "Width=" "height=" "title=" Qrcode_for_gh_ca9759a5ef7f_258.jpg "style=" width:150px;height : 150px; "alt=" wkiol1bfwhsa32xoaadc6s3knxs343.jpg "src=" http://s5.51cto.com/wyfs02/M00/7D/12/ Wkiol1bfwhsa32xoaadc6s3knxs343.jpg "border=" 0 "vspace=" 0 "hspace=" 0 "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1749110
Learn from the zero-based Swift learning Note (day)--swift data type mapping for mixed programming with C + +