Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,944
40
Australia
Hi,

I have a variable called rootPage declared in projectViewController.h and its property has the attributes nonatomic and retain.
Code:
-(void)createItem:(SCItem *)parent:(NSString *)name {
    SCItem *item = [[SCItem alloc] init];
    item.name = name;
    if (parent == nil) {
        parent = item; 
    } else {
        [parent.children addObject:item]; //ignore this
    }
}
Code:
-(void)loadItems {
    [self createItem:rootPage:@"Bookmarks"];
    SCItem *temp = rootPage;
    int k = 0;
}
When the first line of loadItem runs, it passes rootPage (uninstantiated) as the first parameter (parent), the method checks if its nil, and in this case, sets parent to the previously instantiated SCItem item.

The problem is, when I place a break point on the line "int k = 0;", temp and rootPage both show as 0x0 (nil). However, I've placed a break point inside of the createItem method after parent has been set to item, and both show a pointer (data).

How do I make the changes made to parent inside of the createItem method change rootPage and not just some local variable copy inside of the method.

In my primary language (Visual Basic) you'd place ByRef in the parameter instead of ByVal, is there any similar thing here?
 
Before we even go into the various issues with the code you supplied, I would like to ask: what have you already done to educate yourself in the fundamentals of Objective-C / iPhone programming? Books? Videos? Tutorials? Etc? Be as specific as you can when you answer. Thank you.
 
You seem to have a method:
Code:
-(void)createItem:(SCItem *)parent:(NSString *)name;

Which you are calling using the following line:
Code:
[self createItem:rootPage:@"Bookmarks"];

I would fix this for starters. :)
 
You seem to have a method:
Code:
-(void)createItem:(SCItem *)parent:(NSString *)name;

Which you are calling using the following line:
Code:
[self createItem:rootPage:@"Bookmarks"];

I would fix this for starters. :)
Although that syntax is quite rare it is, strangely, valid: the name parameter is just, ironically, unnamed. The method definition could just as easily be written:
Code:
-(void)createItem:(SCItem *)parent :(NSString *)name;
 
OP, are you familiar with pass-by-value and variable scopes in C? If so, the same rules apply to Objective-C.
 
Thanks for all your replies,
Before we even go into the various issues with the code you supplied, I would like to ask: what have you already done to educate yourself in the fundamentals of Objective-C / iPhone programming? Books? Videos? Tutorials? Etc? Be as specific as you can when you answer. Thank you.
Practice, find a problem, look for the answer. I've read some documentation, watched a fair amount of good video tutorials, etc.
I don't own any books, and as much as I'd appreciate suggestions, the most important thing right now is I figure out what to change here to get this working.
You should get a book on Objective-C. That's the best advice you'll get.
Maybe they already have one. That's why I was asking before. No need to be demanding until we get an answer to that.
Unfortunately that isn't the answer I'm looking for, I'm sure a book would be helpful, but right now, I've been unable to continue coding whilst this issue remains, so my time is being wasted, thus I'm quite eager to solve this so I can get on to the next problem.
OP, are you familiar with pass-by-value and variable scopes in C? If so, the same rules apply to Objective-C.
No, but I think I know what you mean by pass-by-value. My native language is Visual Basic -- back then C++ was too hard for me to understand so I started with VB.
Ultimately, in VB, I would add "ByRef" at the start of the parameter instead of "ByVal", to get the desired result, I'm just wondering what the equivalent in Obj C is?

I've posted on the Apple developer forums however the problem wasn't as refined now as it is here, my post wasn't as clear, so I didn't get any answers that solved the problem.
 
No, but I think I know what you mean by pass-by-value. My native language is Visual Basic -- back then C++ was too hard for me to understand so I started with VB.
Ultimately, in VB, I would add "ByRef" at the start of the parameter instead of "ByVal", to get the desired result, I'm just wondering what the equivalent in Obj C is?

Stop. Learn the language and then come back to this problem. I know you consider it a waste of time, but it is nothing compared to the amount of time you are wasting on silly problems like the one you are having at the moment.

Here is Apples free book on Objective-C. I suggest you read it. It is short and should get you to a stage where most of these problems will no longer bother you.

http://developer.apple.com/library/...C.html#//apple_ref/doc/uid/TP30001163-CH1-SW2
 
Stop. Learn the language and then come back to this problem. I know you consider it a waste of time, but it is nothing compared to the amount of time you are wasting on silly problems like the one you are having at the moment.

Here is Apples free book on Objective-C. I suggest you read it. It is short and should get you to a stage where most of these problems will no longer bother you.

http://developer.apple.com/library/...C.html#//apple_ref/doc/uid/TP30001163-CH1-SW2
I don't consider it a waste of time, but I've got work to do and this is how I learn, I set a goal (an app to build), and I finish it; the problems I encounter along the way are how I learn, and the things I learn this way, I don't really forget.

The problem I have with general reading, or, learning the language, is that it isn't what I need right now. The majority of the things I will have to read will be of little or no use to me for my little web browsing app I'm making.

And, a lot of documentation which I read, I don't understand. I'm a very visual learner, I need to put it into practice, and this is the best way for me.
I would appreciate someone telling me the solution to this, I need to get back to work.

(PS: thanks for the link, I will read the document in the meantime)
 
Wow, I'm gobsmacked. You're not being paid to do this work, are you?
Why? I came here for the answer. And no, I'm working for myself; I wouldn't accept a job from someone without knowing how to do it.

I posted here because I needed help with a specific topic, it's apparent several of you know the answer, so why can't you just tell me?
You might see it best that I go and read hours of documentation, but that isn't what I need now; it isn't the time.

As I said, I learn visually and by doing -- when I run into problems, I get help, they get solved, I learn, I move to the next problem. If it doesn't get solved, I get stuck -- I've browsed some documentation several times looking for the specific answer I need, as well as searching the net, and posting on Apple's dev forum, the former two yielded no results, nor the latter, as when I posted, the problem wasn't as refined as it is now.
 
I posted here because I needed help with a specific topic, it's apparent several of you know the answer, so why can't you just tell me?
I could explain the problems with your code but without an understanding of the basic fundamentals of Objective-C, it would probably be meaningless to you. I could just give you the fixed code, but then I'm just doing your work for you, aren't I? So, forgive me if I elect to bow out of this discussion. Good luck, though! :)
 
I could explain the problems with your code but without an understanding of the basic fundamentals of Objective-C, it would probably be meaningless to you. I could just give you the fixed code, but then I'm just doing your work for you, aren't I? So, forgive me if I elect to bow out of this discussion. Good luck, though! :)
Not quite, in either scenario:

1) I'd very likely understand it, and if not, it puts me further ahead in solving it than I was prior.
2) Not quite, I've already written code. All you need to tell me is what to change, that shouldn't require much code to be written, if any at all.

I've been told to replace the 'retain' attribute with the 'assign' attribute, as well as doing [item retain] in the method, amongst other things. Neither of which worked, but the solution should be, as implied by another poster, simple.

EDIT: Anyway, thanks.

EDIT 2:
It's solved now.

Code pretty much resembles this:
Code:
-(void)createItem:(SCItem **)parent:(NSString *)name {
    SCItem *item = [[SCItem alloc] init];
    item.isBookmark = isBookmark;
    item.name = name;
    if (name == @"Bookmarks") {
        *parent = item; 
    } else {
        //left this line out
    }
}
Code:
-(void)loadItems {
    rootPage = [[SCItem alloc] init];
    [self createItem:&rootPage:@"Bookmarks"];
    int k = 0; //break point shows the changes made in createItem
}
 
Again, nope.
I don't think I have any variables left, not that I can see anyway, lol. I'll just have to read the documentation on memory leaks later and then go through my code, I'm sure there are plenty.

Could it be comparing name to @"Bookmarks"? Oh, and I just realised, isBookmark is in my code, it shouldn't be in this case.
 
I intend to ignore all my memory leaks for now, and then, when my app is finished, read up a bit on memory leaks, and then go on a cleaning spree.

This really isn't the best way of going about things. You should learn the fundamentals of memory management first; the rules are quite simple and you'll save yourself a lot of headaches later.

Another note about your code snippet; you should use the isEqualToString method of NSString to compare strings, not ==, which just compares the pointers (it's working by chance rather than design in your example due to the way that NSString literals work).
 
This really isn't the best way of going about things. You should learn the fundamentals of memory management first; the rules are quite simple and you'll save yourself a lot of headaches later.

Another note about your code snippet; you should use the isEqualToString method of NSString to compare strings, not ==, which just compares the pointers (it's working by chance rather than design in your example due to the way that NSString literals work).
1) I'll have a look at the memory management document, thanks
2) Ah, yes, I've had to use that before; thanks.
 
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C5101c Safari/6533.18.5)

MythicFrost said:
Actually, it's one of the ones you've already dismissed.
Lol, rootPage, because its set to item, but is initialised prior. Haha, thanks. I'm going to be rofl if I'm wrong again and its parent lol.

That's correct, sir!

Also, if you're going to go to the trouble of creating a property, you should use the accessors for it. This will help handle some of the memory management as well.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.