Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and set a CGFloat by reference?

Tags:

objective-c

I want to make an method which takes an CGFloat by reference.

Could I do something like this?

- (void)doStuff:(CGFloat*)floatPointer

I guess this must look different than other object pointers which have two of those stars. Also I'm not sure if I must do something like:

- (void)doStuff:(const CGFloat*)floatPointer

And of course, no idea how to assign an CGFloat value to that floatPointer. Maybe &floatPointer = 5.0f; ?

Could someone give some examples and explain these? Would be great!

like image 215
dontWatchMyProfile Avatar asked Apr 22 '10 09:04

dontWatchMyProfile


2 Answers

if you (hate pointers and ;-) prefer objective-c++ pass by reference, the following is an alternative:

-(void) doStuffPlusPlus:(CGFloat &) f
{
   f = 1.3;
}

call by

CGFloat abc = 1.0;
[self doStuffPlusPlus:abc];

and, you need to rename the source filename from ???.m to ???.mm

like image 75
ohho Avatar answered Nov 10 '22 18:11

ohho


If you are passing a CGFloat by reference, then accessing it is simple:

- (void)doStuff:(CGFloat*)floatPointer {
    *floatPointer = 5.0f;
}

Explanation: as you are getting a reference, you need to de-reference the pointer (with the *) to get or set the value.

like image 37
Laurent Etiemble Avatar answered Nov 10 '22 17:11

Laurent Etiemble