开发者

Pointers and data assignment in Objective-C

I'm having trouble dealing with pointers in Objective-C. Basically, I have 开发者_StackOverflow中文版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 ?


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


Use:

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

Example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜