help with syntax

What is the meaning of (NSString *) in the above two lines?

When we want to implement the first method, I guess we write like this:
NSString *string = [textField stringValue];

Or are these two different things?


(NSString *) means, that the return value in the first line and the parameter in the second line are pointers to a NSString class object

this is correct syntax

thanks for the reply.

I am feeling confused.
From the documentation:

If I simply write (without writing “- (NSString *)stringValue” anywhere at all):

the value of the string in textField gets stored in a variable named “string”.

Why is is that what I see in the documentation and what I have written above look so different?

Does the fact that *textField is an instance of the class NSTextField have anything to do with it?

this is just the method definition in the interface (.h file)
which means, it’s a method without any parameter but a NSString object as return (result) value.

The method will be used as a message to the receiver (in your case the NSTextField):

[textField stringValue]

you must define a NSString object to store the return value:

NSString *string;

this line defines the NSString object string, which will keep the string value of the NSTextField

As I wrote earlier, I have not written

anywhere at all.

These are my files:
// CountString.h

#import <Cocoa/Cocoa.h>

@interface CountString : NSObject {
IBOutlet NSTextField *textField;
IBOutlet NSTextField *textField2;


  • (IBAction) countIt:(id)sender;


// CountString.m

#import “CountString.h”

@implementation CountString

  • (IBAction) countIt: (id) sender
    NSString *string = [textField stringValue];
    NSString *formattedString = [NSString stringWithFormat:@“‘%@’ has %i letters”, string, [string length]];
    [textField2 setStringValue:formattedString];

I know, I only wanted to explain the syntax in the documentation

BTW: the return statement in the IBAction method is not needed