Cocoa Questions Thread

whitesaint

cocoa love
Does anybody know how many Mac OS X users (international or english) there? I think it would help me get alot more work done if i knew how many people im reaching out to.

Cocoa is fundamentally objects that perform on other objects.

Here's what its all about: [receiver message];

Or here is a little bit more realistic example: [myWindow close];

This is how set a window half-way tranpsarent in cocoa: [myWindow setAlphaValue:0.5];

If this is too simple for you guys then ask some questions, we'll try and answer them.:D
 
how about preventing memory leaks. in my dealloc method i only release variables that i have retained. is that right? am i correct in assuming if i init and alloc something(say NSDictionary) inside a method(say init) then it is automatically released at the end of the method(init)? i ordered the cocoa by aaron hillegass so i'm sure that will clear things up for me when it arrives.
 
I think its NSObject's init method and any of the init subclass methods you dont have to worry about releasing them. Only if i retain my objects, I usually try to autorelease my objects in the same method definition.
 
Thanks whitesaint, we need a cocoa thread.

Hmmm my problem with learning Cocoa at the moment is that all the tutorials are a little too basic but I still don't know enough about cocoa to do anything really useful. :confused:

I've brought O'Reilly's Learning Cocoa book, and I have learnt how to get Project Builder to write code for me. (I think the writers could have condensed the book to a paragraph.)

I guess there are lots of people in my position. I am a professional developer for another platform.. so I don't really need a book on how to use a GUI - or even a book on the principles of programming.. I need a book/tutorial that assumes I'll figure out the Project Builder and gets right to the meat of programming in OS X.

Any suggestions?
 
Cocoa is fundamentally objects that perform on other objects.

I think it would be better to say Object Oriented Programming is all about objects. In ObjC, you send messages to objects, while in other languages its refered to (although it's really the same thing) as calling methods or functions off of an object. Cocoa is just a set of API's that apple put together that makes it simple to do the hard and tedious stuff, so that programmers can focus on making their apps unique.

Memory management. In all of apple's API's, if you call a method and it returns an object, that object will be autoreleased and have a retain count of 1 (except for stuff like shared instances). Essentially, it will go away as soon as the set of curly braces that the object was created between are passed through. At that point, the NSAutorelease pool checks to see that the object is set to be automatically released, and the object is sent a release message. Now the retain count is 0, and the objects dealloc method is called. If you use alloc before an init* creation or if you send your object a retain message, then after the autorelease pool does its stuff, your object will still have a retain count of 1 or more (depending on how many retains you send your object), and so it will not be totally eliminated.

When you add objects to containers like NSArray's, NSDictionary's, and NSSet's, they are automatically sent a retain message, and when they are removed from the container (either because the container is being deallocated, or because the object is being removed manually), that object is sent a release message (actually, it might be an autorelease message, that might be safer). Therefore, if I alloc and init an object, then add it to an NSMutableArray, that object will not be deallocated after the NSMutableArray is deallocated. I have to send that object a specific release or autorelease message to get rid of it. Just keep track of your retains, allocs, releases, and autoreleases, and make sure your methods are returning autoreleased objects whenever possible, and memory management shouldn't be too much of a headache.

If people need help with Obj-C/Cocoa, just ask! If the tutorials aren't hard enough, ask for some problems. The way I learned all the API's was trying out new programs to make and asking lots of annoying questions on the macosx-dev =)

F-bacher
 
good infos. so if i have some object and it has a retain count of 2+, could i just obliterate it by sending dealloc or is that a bad idea?

to comment on learning cocoa, the way i've been learning is coming up with a project(i wrote an app to replace my checkbook). this helped me b/c i needed to use a lot of common elements like NSTableViews, NSFileManager/Handler, arrays, dictionaries, and string manipulation. so as i developed the project i learned this stuff by putting it into practice.
 
I've never tried sending an object an explicit dealloc message, but it sounds like something fun to try out sometime =D I think it would probably be better to stick to releases and retains. I'm not sure why'd you'd ever want to explicitly call dealloc. If your retain count is soo high that you just want to obliterate it right away, you've probably left it in some kind of container object (like an NSArray), and really should release that object first.

I definetly agree with the trial by fire approach to learning Cocoa. You learn so much by just messing around with all the different APIs. I knew so little two years ago, and now I know so much (or so I think ;))

F-bacher
 
Originally posted by Ghoser777
I've never tried sending an object an explicit dealloc message, but it sounds like something fun to try out sometime =D ...

well, as far as i know you're not supposed to use dealloc directly -- but i could be terribly wrong.

there's only one exception i know of: when you free your own objects, you first release all objects you retained and then send a dealloc to super.

Code:
- (void)dealloc
{
	[myObjectA release];
	myObjectA = nil;
	... go on with everything you retained ...
    [super dealloc];
}

the runtime will release any memory allocated for your application once the application is quit by the user (in contrast to windows...), but that doesn't mean that it deallocates memory while the app is running.
you could slowly fill up the entire memory leading to heavy swapping.

i know of one case where an app was running forever, had a serious memory leak, allocated more and more memory, so that the swap file finally grew so large that there was no space left on the hd. the machine crashed...

kind of a rare case, but it clearly shows that not releasing objects is no good idea.
 
I've been "cocoa-ing" for about 10 months, and looking at the documentation since March 24, 2001. I seem to be able to get most of what I need working, but setting up and adding information to NSTableView has stumped me for a while.

The OmniGroup MacOSX-dev mailing list has some examples, but it looks like they are using some methods that don't work anymore (one wasn't too good, but Scott Anquish wrote one that uses an XML plist in the bundle, but when I try it myself it seems to not load anything. Even when modifying the source app, it refuses to load new data.

To make a long story short, anyone able to write or know where to find an NSTableView tutorial that works with 10.1.4/Dec2001 DevTools?
 
Originally posted by rhale1
To make a long story short, anyone able to write or know where to find an NSTableView tutorial that works with 10.1.4/Dec2001 DevTools?
I'm a newbie Cocoa programming, and I bought "Cocoa Programming for Mac OS X" by Aaron Hillegass. In it he has a good tutorial on how to use NSTableView. Also, in Mike Beam's Cocoa articles, he has a tutorial that shows how to use tables. His articles have helped me out a lot. I would suggest reading them (there's a lot of good ones).

While I'm at it, I need some help to. First, I need to search through a directory's contents, and see if within one of those directories there is a certain file that exists. So, I have a directory (for example) that contains these directories: Mammals, Amphibians, Fish. Now in each of these directores, I want to see if there is a file that is called "Eggs" that exists in them. Does anybody have any code that could do this?

Also, one more thing. This is probably pretty easy, but does anybody know how to convert a text so that it works as a URL. Like if I had a string that contained "My dog is my best friend" how could I convert that so it looks like "My+dog+is+my+best+friend" and all the other characters that need to be converted? I know how to do generic replacing, but there's probably some kind of official method.

Thanks for anyone who helps,
Kevin
 
Originally posted by aishafenton
I have learnt how to get Project Builder to write code for me. (I think the writers could have condensed the book to a paragraph.)

Hello!

Can someone fill me in on how to do this? This might be helpful if it is what I think it is. Is it some kind of autocomplete thing?

Thanks!

Albert
 
I think, although I could be wrong, that he's talking about right-clicking on a class you've created in IB and added outlets and actions to, and then selecting "Create Files" or whatever it says. It will create your header and implementation files with references to all your outlets and method declarations already done.

I prefer Writing all my code with IBOutlet and IBAction's so that I can right click on my class in IB and choose "Read File" and have all my actionas and outlets get sucked out of my source code and be put into IB, but that's just me.

F-bacher
 
Hello!

Ahh, OK. Yeah, I knew about that feature, I was thinking and hoping it was some kind of autocomplete feature.

I really think it would be cool is Project Builder had an autocomplete feature like in REALbasic. So instead of having to type "[receiver message];" you could type just part of the code and a "…" would show up. Then you just hit tab to see a list of what could be put there. That is one of the ways I learned RB so fast was just using the autocomplete. I really think PB should have this as well. :)

Have a great day!

Albert
 
I am new to Cocoa and I am porting a Windows program to the Mac.

I have two questions;

1) How do I launch a URL from my program, so it opens in the defaut web browser (in windows it is like ShellExecute(...,"http://www.mylink.com",...");

2) How do I make the file extensions in the standard NSOpenPanel case insensitive, so the extension "obj" is equivalent to "OBJ" or "Obj"?

Any help would be greatly appreciated,
Konan
 
Originally posted by konan
1) How do I launch a URL from my program, so it opens in the defaut web browser (in windows it is like ShellExecute(...,"http://www.mylink.com",...");[/B]
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:mad:"http://www.mylink.com"]];

Put that code where ever you want to load the website.
 
Originally posted by martinatkinson
Ahh, OK. Yeah, I knew about that feature, I was thinking and hoping it was some kind of autocomplete feature.

well, there is. it's not too advanced, but it works. type the first few letters of the name of a variable, method, whatever and hit f5. if you have a few variables all beginning with the same first letters, type as many letters as it takes to make the name unique, otherwise it'll just complete it with a random (well, that's my impression) possibility.
 
Thanks kainjow, that was really helpful. In the end, I decided to use the following

[[NSWorkspace sharedWorkspace] openFile:mad:"http://www.mylink.com"];

since it was more generic and could be used for any kind of document.

Thanks a lot for your help :D,
Konan
 
Back
Top