Apples documentation syntax explanation

Hey,

I’m struggling a little with this chapters challenge 1, which could be because of one of 2 reasons…

  1. I’m being really dumb
  2. Lack of sleep due to the 6 week old baby we have, and my head isn’t “with it” today

I’m not struggled up until now, but I’m just having trouble understanding Apple’s documentation syntax a little. I don’t want to use the forums to just look up the answers, as that doesn’t really help learn anything.

Just a suggestion maybe for a future release of this book… A quick section on how to interpret Apple’s documentation/parameter syntax? This may be straight forward to most people reading this book, in which case reason 1 above is probably the culprit, and you can ignore my request.
But I know I’d find it really beneficial having a breakdown of the syntax of the code samples used in their documentation.

Or am I just supposed to be able to “get it” if I’ve made it this far in the book :slight_smile:

Just as a side note, this is by far the best Objective-C programming “course” I’ve come across. Been flicking through a few different ones, but I settled on this one as you have us coding right from the word go… Definitely the best way to learn!

/sep

After coming back fresh to this after a half decent nights sleep, something clicked, and I managed to figure this out.

I still think a breakdown of the parameter syntax could be good, but number 2 from my original post was the cause it seems :slight_smile:

I would have to second this. I am struggling as well. I just don’t get simple things like how this:

  • (NSRange)rangeOfString:(NSString *)aString

turns into this in code:

NSRange match = [listOfNames rangeOfString:name];
if (match.location == NSNotFound) {
NSLog(@" No match found!");
// Other actions to be taken
} else
{
NSLog(@" Match found!"); // Other actions to be taken
}

I know I’m probably missing something super simple here.

[quote]I would have to second this. I am struggling as well. I just don’t get simple things like how this:

[/quote]
That’s how an instance method is declared in Objective-C.

[quote]turns into this in code:

NSRange match = [listOfNames rangeOfString:name]; if (match.location == NSNotFound) { NSLog(@" No match found!"); // Other actions to be taken } else { NSLog(@" Match found!"); // Other actions to be taken }[/quote]
The first statement is invoking the method on an object named listOfNames, and the next statement, an if-statement, is testing the result of the method invocation.

[quote]I know I’m probably missing something super simple here.
[/quote]
You are not missing anything; the book implicitly (and rightly) assumes that you can write code in at least one language, especially in the C Programming Langue.

[Accelerate your learning and become a competent programmer: pretty-function.org]

Thanks for that reply.

I think I understand that this is how an instance method is declared.
So

listofnames is the receiver, and the selector is rangeOfString and the argument is :name right?

But how do I know when that little bit there, the receiver, selector, and argument, should be preceded by something like:

NSRange match =

Is it because I want the result to be placed into the variable result so that I can print it out or something with NSLog?

I guess I just don’t get what the documentation is telling me in terms of how to use it.

I really am thankful for your response but page 3 of the book does say

so I assumed that I do not have to know another language. I have however dabbled in js and c. I really am not that good, and that’s probably the issue here. So, that being said, is there somewhere better for me to start?

Thanks for the help and guidance. This is a VERY humbling experience for me. Seriously.

Brandon

Brandon,

I have learned Cococa and iOS Programming by reading all BNR Books - They are excellent books.
However, to fully benefit from them, one must have a good degree of programming experience. In my case I had that experience.

However reading those books are not enough to thrive in the Cocoa and iOS world, then there is the mountain of Cocoa and iOS documentation waiting to be conquered for the frameworks used to build applications.

If you truly want to be a good programmer, you should first learn the fundamentals of programming in a simple yet powerful language. That language is the C programming Language.

So start with the C programming Language first because Objective-C is based on it. Once you have done that Objective-C and even Swift will be a piece of cake!

PS: I specialise in teaching C, C++, and Objective-C Programming languages; you can contact me through my website: pretty-function.org

You didn’t miss anything. The syntax doesn’t seem straight forward, understood. Almost nothing in obj-c does. However when you look at apple’s documentation what’s explained is the overview(purpose of said method or function) and it’s uses but not necessarily how to use them, yes there’s code examples but they only explain so much. Let’s take a look at the method again,

  • (NSRange)rangeOfString:(NSString *)aString

as we learned in the book, the first part of any method is whether or not the method is an instance method or class method, well, we can tell by the - sign in front that it is indeed an instance method so right off the bat we know in any given message send, it goes in the selector side of the message send([receiver selector:arguments if any];). The next part (NSRange), tells us that it returns a type value of NSRange. (read the documentation on NSRange) next, rangeOfString: is the name of the method, that’s what you’d enter in the reference API search to find said method, next is the argument it takes, in this case a pointer variable that hold an NSString. or as it says aString.
So in practice,
NSString *coolNames = @“Andy, Aaron, Hillegass, Martin, Luther, King, Steve, Jobs”; //here’s a basic string of delimited characters. separated by commas. Let’s find luther.
NSString *name = @“Luther”; //name were looking for, oh and it’s case sensitive so not luther but Luther notice the l is capital.
// next,
NSRange match = [coolNames rangeOfString:name];//now, we’ve placed coolNames before rangeOfString, why? remember rangeOfString, is an instance method it has to act on something doesn’t it? It has to act on an instance of NSString just like the instance stored at *coolNames. “name” goes in the argument, because that’s the parameter required to utilize rangeOfString…it returns a yes or no, if you will again read NSRange overview and NSNotFound…

I hope this helped a little.

[quote=“SmallNerdRancher”]You didn’t miss anything. The syntax doesn’t seem straight forward, understood.

I hope this helped a little.[/quote]

It helped a lot, and was infinitely more useful than an earlier answer: “You are not missing anything; the book implicitly (and rightly) assumes that you can write code in at least one language, especially in the C Programming Langue.”, particularly given that this book explicitly states that it will teach you the basics of C as needed for Objective C, AND the question asked has nothing to do with C.

Your answer was empathetic and useful. Thanks.