How to create a pointer in Swift? -
i'm working swift 3.
i have c syntax :
int myvar; int *pointer = &myvar;
so modifying pointer
or myvar
same exact same thing. don't know if makes difference, in case myvar
array containing elements of class , pointer
pointer 1 element of array.
(assuming understand you're asking for....)
try following code in playground. should print "99" 3 times.
class row { var rownumber = 0 } var rows = [row]() let testrow = row() testrow.rownumber = 1 rows.append(testrow) let selectedrow = rows[0] selectedrow.rownumber = 99 print(testrow.rownumber) print(selectedrow.rownumber) print(rows[0].rownumber)
by default, there's no copying of objects part of assignment statement. if struct, different.
adding bit completeness:
if want similar effect scalar values instead of objects, swift supplies various types of wrappers.
let intpointer = unsafemutablepointer<int>.allocate(capacity: 8) // should 1, not 8 according comment re: docs let other = intpointer other.pointee = 34 print(intpointer.pointee)
(warning: haven't used these wrappers except experimenting in playground. don't trust without doing research.)
Comments
Post a Comment