1

I'm having trouble dealing with pointers in Objective-C. Basically, I have the following structure in my class :

UITableView *list;
NSArray *objArray;
UIPickerView *pickerCtrl;

My "list" shows the data contained in objArray, which is a temporary structure linking to custom NSObjects of various types (not stored in my current object).

Choosing one element in the list shows the "pickerCtrl", displaying appropriate data depending on which TableView line is currently selected.

My goal is to replace oldObject's data (the external object, accessed by objArray) with newObject's data (selected in the PickerView). Like this :

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    id oldObject = [objArray objectAtIndex:[[list indexPathForSelectedRow] row]];
    id newObject = [pickerData objectAtIndex:row];

    *oldObject = *newObject;
}

From the debugger, oldObject and newObject both have the right memory addresses. The problem is, no assignation seems to be done, and the old data is never replaced by the data from "newObject".

What am I missing here ?

2
  • Well, I gave up with those pointers and decided to proceed in a more simple way. I just implemented a updateValues:(id)newObject method in my custom objects and call it : [oldObject updateValues:newObject]; Don't know why I didn't think about it earlier and got crazy with those pointers. Commented May 26, 2011 at 13:49
  • it seems like you where thinking "too" efficient =) Commented Jun 6, 2011 at 17:59

2 Answers 2

1

This is not the proper way to deal with mutable arrays, you are thinking too low-level.

Rather, try this:

[objArray removeObject:oldObject];
[objArray addObject:newObjec];

You can also use the insertObject:atIndex: method. See the reference for NSMutableArray for further information

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. However, this is not really what I want to accomplish. "objArray" is just a temporary structure pointing to my actual objects. Therefore I need to change the objects themselves, not just the data contained in the array.
1

Use:

- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2

Example:

[objArray exchangeObjectAtIndex:[[list indexPathForSelectedRow] row],row];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.