views:

58

answers:

2

Hi,

Apologies for how basic these questions are to some. Just started learning Cocoa, working through Hillegass' book, and am trying to write my first program (a GUI Cocoa app that counts the number of characters in a string).

I tried this:

NSString *string = [textField stringValue];
NSUInteger *stringLength = [string length];
NSString *countString = (@"There are %u characters",stringLength);
[label setStringValue:countString];

But I'm getting errors like:

Incompatible pointer conversion initializing 'NSUInteger' (aka 'unsigned long'), expected 'NSUInteger *'[-pedantic]

for the first line, and this for the second line:

Incompatible pointer types initializing 'NSUInteger *', expected 'NSString *' [-pedantic]

I did try this first, but it didn't work either:

[label setStringValue:[NSString stringWithFormat:@"There are %u characters",[[textField stringValue] length]]]

On a similar note, I've only written in easy scripting languages before now, and I'm not sure when I should be allocing/initing objects and when I shouldn't.

For example, when is it okay to do this:

NSString *myString = @"foo";

or

int *length = 5;

instead of this:

NSString *myString = [[NSString alloc] initWithString:"foo"];

And which ones should I be putting into the header files?


I did check Apple's documentation, and CocoaDev, and the book I'm working for but without luck. Maybe I'm looking in the wrong places..

Thanks to anyone who takes the time to reply this: it's appreciated, and thanks for being patient with a beginner. We all start somewhere.

EDIT

Okay, I tried the following again:

[label setStringValue:[NSString stringWithFormat:@"There are %u characters",[[textField stringValue] length]]]

And it actually worked this time. Not sure why it didn't the first time, though I think I might have typed %d instead of %u by mistake.

However I still don't understand why the code I posted at the top of my original post doesn't work, and I have no idea what the errors mean, and I'd very much like to know because it seems like I'm missing something important there.

A: 

(For some reason I can't edit my original post.)

Okay, I tried the following again:

[label setStringValue:[NSString stringWithFormat:@"There are %u characters",[[textField stringValue] length]]]

And it actually worked this time. Not sure why it didn't the first time, though I think I might have typed %d instead of %u by mistake.

However I still don't understand why the code I posted at the top of my original post doesn't work, and I have no idea what the errors mean, and I'd very much like to know because it seems like I'm missing something important there.

Matt
I edited this into your post for you. Not sure why you couldn't get it to work, but it's in there now.
Chuck
Completely new Stackoverflow user's can't edit their own post until they get some rep. I presume it's a spambot prevention method.
TechZen
+4  A: 

The first problem is the fact that -[NSString length] returns an NSUInteger, not an NSUInteger *. The first is an integer; the second is a pointer to an integer (more on that in a second). The second problem is that this line:

NSString *countString = (@"There are %u characters",stringLength);

Doesn't do what you expect. I think you want to format a string, but what this is actually doing is executing the expression @"There are %u characters", then stringLength, and returning the value of the second expression. What you want is this:

NSString *countString = [NSString stringWithFormat:@"There are %u characters", stringLength];

Which you said didn't work, but I'm not sure why.

For example, when is it okay to do this ... instead of this:

It's "okay" when you're setting a variable of that type to return value of the same type. In C (and Objective-C is just some stuff on top of C), there are values and then there are pointers to values. Pointers to values are actually memory addresses that point to a value in memory. For example, this is a value of type int:

int x = 5;

And this is a pointer to that value:

int *xptr = &x;

x has the value 5. xptr has some memory address as its value. *xptr (the dereferenced value of xptr) has a value of 5, since xptr points to a value of 5 in memory.

This is not something you have to worry about much in Cocoa. The API docs will tell you the return values of methods and functions, and Objective-C objects are always pointers. There are a few types to watch out for, though: NSRect, NSPoint, and some other values are just C structs (not Objective-C objects) and thus sometimes you deal with the values themselves, and not pointers. And things like NSUInteger are generally not pointers, either (but sometimes they are).

mipadi
Thanks for your reply--it's cleared up a lot for me.
Matt