PDA

View Full Version : newbie questions on Cocoa, objective-C, etc.




Boneoh
Feb 27, 2009, 01:32 PM
Hi, all!

I am a newbie on Mac programming, most of the past 15 years has been VB .net, VB, SQL Server, etc. on Windows. Another 15 years before that in mainframe, minis, supercomputers using Assembler, Fortran, and Pascal. I have to admit that the Cocoa and Objective-C learning curve has been tougher than I thought it would be. I am very encouraged by what I am seeing in terms of the Cocoa/Xcode toolset, environment, etc. I think that once I get up the curve, I will be able to create my applications quickly without a lot of the nuts and bolts that have been a pain in VB .net for a long time.

I am working through a few books that have been very helpful: Cocoa Programming for Mac OS X by Hillegass, Beginning Mac OS X Programming by Trent + McCCormack, and Beginning Xcode by Bucanek. These are all giving me a lot of info and examples, but I am still a bit confused as to understanding some points.

1. Using Core Data and the data modeler, I have created some nice models. This is way cool. The example of creating an Instant Interface was an eye opener. The question I had about this was, the model classes did not appear to generate any code for -init and -dealloc. Yet the auto generated program worked fine! Where is the magic mojo in this that I am not understanding? When I use these model classes 'in real life' with my code, am I correct in assuming that I need to have implementations for -init and -dealloc? I would think so.

2. In all of the examples, the emphasis is on using the Cocoa classes for things, such and NSNumber, NSString, etc. rather than using the similar C language equivalent. I somewhat understand how using the Cocoa classes helps things like Core Data, bindings, KVC, and KVO glue things together. This is cool. The question I have about this when should I use the C language in my coding. For example, I have some nested For loops to create a bunch of object instances. The number of iterations is entered by the user on the window. It seems that this would be an NSNumber. When I code the For loop, should I be indexing using an int or NSNumber?

Thanks!



kainjow
Feb 27, 2009, 02:03 PM
1. Using Core Data and the data modeler, I have created some nice models. This is way cool. The example of creating an Instant Interface was an eye opener. The question I had about this was, the model classes did not appear to generate any code for -init and -dealloc. Yet the auto generated program worked fine! Where is the magic mojo in this that I am not understanding? When I use these model classes 'in real life' with my code, am I correct in assuming that I need to have implementations for -init and -dealloc? I would think so.
Are you referring to your own NSManagedObject subclasses?

2. In all of the examples, the emphasis is on using the Cocoa classes for things, such and NSNumber, NSString, etc. rather than using the similar C language equivalent. I somewhat understand how using the Cocoa classes helps things like Core Data, bindings, KVC, and KVO glue things together. This is cool. The question I have about this when should I use the C language in my coding. For example, I have some nested For loops to create a bunch of object instances. The number of iterations is entered by the user on the window. It seems that this would be an NSNumber. When I code the For loop, should I be indexing using an int or NSNumber?
You would use a normal int here. NSNumber is more useful for storing primitives inside containers such as dictionaries and arrays. In this instance, you'd want to use intValue on your NSTextField. NSNumber isn't even needed.

Boneoh
Feb 27, 2009, 03:43 PM
Hi, kainjow. Thanks for your help.

For question 1, yes, these are my subclasses of NSManagedObject.

I'm wondering how this hangs together with the automatically generated window. It seems to me that in order for me to use these classes, I would need to add -init and -dealloc to them in order to retain and release the instance variables, correct?

For question 2, it seemed that using a simple int was the way to go. The examples in the books are really focused on certain aspects, and sometimes the simple things seems to be left unsaid.

Pete

GorillaPaws
Feb 27, 2009, 06:56 PM
In response to #2, you will use things like NSNumber when your working with classes that expect objects and not primitives. NSNumber just wraps up a primitive and makes it an object so you can use it with those classes.

Boneoh
Feb 28, 2009, 01:08 PM
I found the answer to question 1 about core data and NSManagedObject.

Here is a clip I found while googling:

The to-many relationship accessors are declared in a category, but there's no implementation. Core Data dynamically generates the implementations for you at runtime. The declarations provided in the header file make sure you don't get compiler warnings when you invoke the methods.

I guess my initial question lead more into my confusion around:

@interface Network (CoreDataGeneratedAccessors)

I was wondering if I had to implement the above accessors that were generated in the interface. The answer is usually no. If there is any special processing needed, I can always override it.