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