Swift and Pointer primary use

Source: Internet
Author: User

Swift contains C,cint, CBool, and CChar in front of the underlying type of C

unsafepointer<cchar> Const char * for C, constant pointer not variable

unsafemutablepointer<cchar> char * for c; variable pointer

The following first behavior string turns char*; second behavior char* to string

Let SSS = unsafepointer<cchar> ("char string: " . Cstringusingencoding (nsutf8stringencoding)) Print (string.fromcstring (SSS) )

For example, the following uses the C language objc/runtime traversal of all member variables in the current class, using Unsafemutablepointer<uint32>, corresponding to c inside unsigned int *varcount

Let varcount:unsafemutablepointer<uint32> = Unsafemutablepointer<uint32>.alloc (1) Varcount.initialize (0) Let Classvars=class_copyivarlist (Self.classforcoder, Varcount) forIinch 0... (Int (Varcount.memory)-1) {Let C:ivar=Classvars[i] Let name=Ivar_getname (c) Let NCC= unsafepointer<cchar>(
Let NSC=string.fromcstring (NCC) print (NSC)
Let type=ivar_gettypeencoding (c) Let TCC= unsafepointer<cchar>(type) let TSC=string.fromcstring (TCC) print (TSC)}free (Classvars)

It can be written in OC.

unsigned count =0; Ivar*vars = Class_copyivarlist (c, &count); for(inti =0; I < count; i++) {Ivar v=Vars[i]; Const Char*name =Ivar_getname (v); Const Char*nametype =ivar_gettypeencoding (v); NSLog (@"==%s==", name); NSLog (@"==%s==", NameType); NSString*sname =[NSString stringwithcstring:name encoding:nsutf8stringencoding];} Free (VARs);

Unsafepointer<t> in Swift can use. Memory to get the value pointed to by the pointer

Let ss:unsafepointer<cchar> = unsafepointer<cchar> ("b". Cstringusingencoding (nsutf8stringencoding)) NSLog ("%c-%d", Ss.memory)

Swift and Pointer primary use

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.