• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with my first Objective-C class

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic