GeeCON Prague 2014*
The moose likes Other Languages and the fly likes Help with my first Objective-C class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Help with my first Objective-C class" Watch "Help with my first Objective-C class" New topic
Author

Help with my first Objective-C class

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hey,
This is my first Objective-C class:

Upon compilation, I got those errors:
At line 18 : incompatible types in return
At line 19: warning: control reaches end of non-void function
//Ignore the error at the line 23, I know I'm making an error
Any ideas what I'm doing wrong?
BTW, I know I have to create a reference this way:
*reference
not like Java:
reference
But why I have to write something like this:
(NSString*)
Why not just NSString?
Thanks for help and time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
If it is anything like ordinary C, then the * means what you are passing is the contents of a pointer, so isbn is a pointer to the String which you are calling isbn. If you print isbn with the (I think) %p flag, you get the actual memory location where "isbn" starts, and you can print the individual characters like thisSo your method obviously returns a pointer; you may need to cast it. When you write (NSString *) it looks as if you are declaring the return value as type NSString pointer. But you are returning the contents of that pointer, which is probably the first character of the NSString. Try taking the * off "return *isbn;"

Is that really your first Objective-C class? Why are you using pointers at such an early stage?
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Would you please consider this version:

isbn getter method returns a pointer to NSString (this is indicated as the return type), why I'm returning back isbn instead of *isbn (which it is the pointer)?

In setter method I wrote:
isbn = newIsbn;
Why it is not:
*isbn = newIsbn;

If *isbn is a pointer to NSString, what is isbn (the instance variable, not the getter method) then?
Well yes it is my first class
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
The * operator in C (and I presume similarly in Objective-C, though I have never used that latter language) gets the contents of the pointer. If you write int *i; you say, "i is a pointer to an int." A lot of people recommend you put a "P" or "Ptr" or similar as part of the variable name, to remind yourself it is a pointer.Try that
campbell@xxxxxx-laptop:~/CPrograms$ pointer 123 Campbell
You can see that memory space . . .
Note that what you pass is a pointer to a pointer (usually called a handle) called argv. That is actually an array of arrays of C-type chars, so if you look in argv + 1 you get a pointer to its second element (a string like "123 ") which actually has a null character ('\0') where I put the space. The atoi function reads that String until it reaches the null character and parses it to an int. You can see the location that int is put in, and also its contents. The pointer i is the location in memory and *i is the contents of that memory, in this case 123.
You can get the location of the second string entered with the * operator like this *(argv + 2). [Note that *argv points to the 0-th String which is "pointer ".] So the contents of that memory location is where you find the first character of that String. In the case of "Campbell" it occupies 9 locations, 8 letters and the null. If you use the %s tag, it prints all the characters it finds until it reaches the null. Note you can turn it back and forth between int and char * (pointer to char) by casting. If you get a char from *((char *)j) you can print it out as 'C'.

In the case of your isbn variable, it is a pointer to char or String or similar. If you put 0123456789 in, then you get a pointer to the location of the first '0', ie its memory location. You want to return that memory location. If you return *isbn, then you return 0x30 (=48 = ASCII value for '0' character), which is not what you want.

I hope this makes some sort of sense, and good luck with it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
I earlier wrote:Is that really your first Objective-C class? Why are you using pointers at such an early stage?
You see what I mean? Why have you got such complicated stuff at such an early stage?
 
GeeCON Prague 2014
 
subject: Help with my first Objective-C class