views:

442

answers:

2

Hi Everyone:

I am looking for a way to store references to variables inside a NSMutableArray. As variables are going to be created dynamically based upon what the user has chosen, I want to be able to simply sort through this array and get references to these created variables. In case it matters, I am creating a iPhone project. However, when I attempt to do this using the following code:

for (int a = 0; a < 10; a++)
{
UIView *tempView;
[listingOfViews addObject:tempView];
}

for (int a = 0; a < [listingOfViews count]; a++)
{
[listingOfViews objectAtIndex:a] = defaultViewStructure;
}

It gives me this error: "error: lvalue required as left operand of assignment"

Any ideas?

EDIT: Also, I get this error: "error: request for member 'size' in something not a structure or union"

When I attempt to do

for (int a = 0; a < [listingOfViews count]; a++)
{
[listingOfViews objectAtIndex:a].size.height = ...
}
A: 

What you mean is:

[listingOfViews replaceObjectAtIndex:a withObject:defaultViewStructure];

But it'll crash when you try to put those uninitialized tempView pointers into the array. What are you really trying to do here? I think it may be time to step back and start with the basics of Objective-C. It looks like you're trying to port C++ concepts directly into ObjC, and they do things very differently.

You may want to start with Programming in Objective-C, and then probably Cocoa Programming for Mac OS X. (The latter is Mac-based, rather than iPhone, but it's still the best starting point.)

Rob Napier
Hi Rob:I don't quite want to replace the object, simply set the object's value to the value of the other object. This works fine when I do it without using a NSMutableArray. In addition, the code above is simply an example of what I'm trying to do (my actual code is longer) - In the real case I am initializing the values of the UIView.
PF1
What do you mean by "set the object's value?" A UIView does not have a value. You do, however, have a pointer to a UIView (which is what you want), and so you can replace the pointer in the array with a pointer to a different object, which is what I assume you want, and what this method would do.
Rob Napier
Rob Napier
Hi Rob: Sorry I'm not phrasing this correctly - So I'll attempt to express it with code: "UIView *tempView = defaultViewStructure" works just fine. However, when you try to get the pointer to that tempView from the array, it returns that error. Does that make sense, sorry if it doesn't...
PF1
Whoops, reposted that to correct something just as you posted that... Sorry.
PF1
I suppose I am expecting to set the variable referenced in the array to another variable. In other words, "copying" another variable and "pasting" it under the name of the variable in the array.
PF1
What you're asking for is a pointer to a pointer (which are not rare, but not very common in ObjC). All you're putting in the array are pointers, which you created by making copies of other pointers. What you're asking for is a pointer to the original pointer (UIView** rather than UIView*). It's legal, but don't do this. You're way off track of how ObjC is written, and it's going to make things way too complicated and difficult to debug (your design causes "spooky action at a distance"). Go back to the original problem you really want to solve, and study how ObjC does these things.
Rob Napier
A: 

To your second question:

for (int a = 0; a < [listingOfViews count]; a++)
{
    [listingOfViews objectAtIndex:a].size.height = ...
}

There's a misunderstanding here (a UIView does not have a -size, it has a -frame that includes a size), coupled with something that is actually a little tricky because of the very confusing dot notation they added in Obj-C 2 and an unfortunate breakdown of the object model when you get to structs. The way you do what you're trying to do is:

UIView *view = [listingOfViews objectAtIndex:a];
NSRect frame = [view frame];
frame.size.height = ...
[view setFrame:frame];

There are ways to condense the number of lines slightly, but you do need to fetch an NSRect struct, modify it, and then set it back. You can't modify the height in one call on most views.

Rob Napier
Hi Rob: Thanks for your reply. However, even using code like: [listingOfViews objectAtIndex:a].center = ... still generates that error. And will the code that you provided re-save the pointer located in the array with the changes?
PF1
The dot notation is causing you the problem. -objectAtIndex: returns an 'id' (a generic object pointer), so the compiler can't figure out what to do with the ".center". If you code it the way I recommend above, the compiler will understand it. The code I provided does not need to "re-save the pointer." The pointer points to something else. It does not change. The thing pointed at changes when you call setFrame:. I'll repeat my recommendation that you look at the books I listed above or the Stanford iPhone course, or some other basic training in ObjC. These are fundamental concepts to ObjC.
Rob Napier