Objective-C Pointers> Pointing to Properties

I have an NSInteger property of a custom class called "approximate time", now in my UITableView class I am trying to pass this property as a pointer to a UITableViewCell. I can't seem to get it to work! I tried the following:

NSInteger *pointer = sharedManager.tempTask.&estimatedTime; NSInteger *pointer = &sharedManager.tempTask.estimatedTime; 

I get errors: lvalue is required as a unary '&' operand and: expected identifier before '&' Marker

Can you pass a pointer to a property? Is this property not only because it points to ivar in my regular class? I need it as a pointer type, so I can edit the value when the UITextField changes inside the UITableViewCell.

Thanks and hope this makes sense!

+4
source share
4 answers

Properties are not variables; it's just syntactic sugar for the get / set-style methods. Therefore, you cannot take the property address.

+14
source

As Marcelo said, you cannot do this using the property itself.

You will have to either:

  • Add a tempTask method that returns a pointer to estimatedTime iVar ( NSInteger *pointer = sharedManager.tempTask.estimatedTimePointer )
  • Use a temporary NSInteger , taking its address for any calls you need, then copy the result to estimatedTime

Option 1 is probably a very bad idea , as it destroys the encapsulation of objects.

+3
source

To use numbers in pointers, I would suggest using NSNumber * rather than NSInteger * (NSInteger is really int). For example, if sharedManager.tempTask.estimatedTime is NSInteger, you can do:

 NSNumber *pointer = [NSNumber numberWithInt:sharedManager.tempTask.estimatedTime]; 

Now that you want to use the int value for the do number:

 NSInteger i = [n intValue]; 

NSNumber * is an obj-c object, so the usual save / release / autorun mechanisms are used.

0
source

Actually, when you say Object1.Propertyxy.Property1

It is actually called FUNCTION, not VARIABLE / VALUE in some memory.

In your case, "tempTask" will act as a function and "estimated time" as an argument, and the result will be a RETURN function.

I know and completely agree that pointers are very favorable for increasing speed, but in this case it is simply useless, since it will require storing that PROPERTY somewhere and after that refers to i, just a waste of time and memory, if you need to use this very specific property 100 times per turn: D

Hope this helps, if it would not just let me know, I will be happy to help.

0
source

Source: https://habr.com/ru/post/1339807/


All Articles