Basic problems (type inference or something else?) in Objective-C/Cocoa.

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-04-30T16:49:04Z Indexed on 2010/05/01 23:57 UTC
Read the original article Hit count: 237

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa