0

I've seen another question about this (Understanding how to use methods found in Apple Developer Reference documentation), and it looks like it was closed for not being specific enough, so I wanted to ask a specific question to see if I could get at the same answer they were trying to get at.

I'm newer at this, and want to be able to read the Apple Documentation better, but I can't instantly see how I would know this (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/#//apple_ref/occ/instm/NSArray/initWithObjects:count:):

- (instancetype)initWithObjects:(ObjectType)firstObj

turns into (something like) this (as an example):

rssFeedURLs = [[NSMutableArray alloc] initWithObjects: url1, url2, nil];  

I'm sure its easy once it "clicks", so I was wondering if you could give me any help so that I can really understand this at a basic level a little easier.

I'm working my way thru programming books, tutorials, videos, etc right now to keep diving deeper into understanding foundations and basics. For whatever reason, I'm getting hung up on understanding Apple Docs methods, in terms of translating it to actual code.

Thanks for your patience and time!

Community
  • 1
  • 1
Com
  • 7
  • 4
  • 2
    If you look at the "parameters" section of a given method, you'll usually find a better description of what each parameter does/how to use it. For example, for `initWithObjects:`, the docs say "*A comma-separated list of objects ending with nil.*" – which is pretty helpful in your case. Unfortunately, this question is still primarily opinion based (everyone learns differently), and therefore I'll be voting to close. – Hamish Apr 19 '16 at 15:55
  • So `NSArray` is the `instancetype`, but why do they put it in `()`? `initWithObjects` is the method. You pass an `ObjectType` into it. That's the sort of thing I'm trying to understand fully, how to explain it to myself as I go along – Com Apr 19 '16 at 16:18
  • [`instancetype`](http://stackoverflow.com/questions/36020540/why-is-instancetype-used) is the type of value the method returns (in this case a new `NSArray` instance). All method return types are enclosed in `()`, that's just the obj-c syntax. `initWithObjects:` is the name of the method, and `ObjectType` is a [lightweight generic type](http://stackoverflow.com/questions/848641/are-there-strongly-typed-collections-in-objective-c), which represents the type of objects to store in the array. – Hamish Apr 19 '16 at 16:32
  • Thats very hepful, sorry my question doesn't fit the parameters maybe it should, but thanks for being nice about it. As a follow up, how would I make this question less opinion based? I put a specific example in, but that must not be what you're looking for. Thanks again! – Com Apr 19 '16 at 17:46
  • The problem with the question is no matter how specific the example, the underlying question of "How do I better understand the Apple docs" is opinion based. I'm not really sure there's a way to make your question not opinion based. I would suggest trying to build a simple app in obj-c, using SO to answer any *specific* questions/problems you might have about your code. Just make sure it's [on topic](http://stackoverflow.com/help/on-topic) and you do some research before asking :) – Hamish Apr 19 '16 at 18:38
  • @originaluser2 I've actually built a few simple apps, but I feel like its been piecemealing parts here and there, or using brute force, etc. I have a understanding of how things work on a shallow level, or if I see the code; but to look at Apple Docs and that translate to an understanding of the pieces is what I'm still missing. That's where I'm trying to get, and trying to find the bridges to get there. – Com Apr 19 '16 at 18:53

2 Answers2

1

You haven't read carefully. There is actually

- (instancetype)initWithObjects:(ObjectType)firstObj, ...;

in documentation. ... is variable arguments. To understand something like this, you should know what it is exactly. So, in this case, it comes to understanding language features. Talking about ObjectType, it is lightweight generics, that is also language feature.

If you want to know, why in url1, url2, nil there's nil at the end, you should carefully read documentation. From referenced by you

A comma-separated list of objects ending with nil.

So, no any magic here. In summary, you need two things to better understand Apple reference

  1. Understand language you're using
  2. Read carefully without missing a word
Sergii Martynenko Jr
  • 1,407
  • 1
  • 9
  • 18
  • So `NSArray` is the `instancetype`, but why do they put it in `()`? `initWithObjects` is the method. You pass an `ObjectType` into it. That's the sort of thing I'm trying to understand fully, how to explain it to myself. – Com Apr 19 '16 at 16:17
  • `instancetype` is language feature. It tells compiler, that method will return object of class on which you call it. Putting something into `()` is ObjC basics - it is return type of method (Read your thousands of tutorials once more). No, you don't pass ObjectType into method - http://stackoverflow.com/a/30719796/5016443 – Sergii Martynenko Jr Apr 19 '16 at 16:35
  • Appreciate the response. Which is this referring to "Read your thousands of tutorials once more"? – Com Apr 19 '16 at 17:44
  • You're welcome! "Read your thousands of tutorials once more" is about yours "I'm working my way thru programming books, tutorials, videos, etc ". Look's like you're struggling with Objective-C basics, so be sure to get better knowledge about language, than return to frameworks documentation – Sergii Martynenko Jr Apr 19 '16 at 17:50
  • Oh ok now I see you were making a joke at my expense, I didn't pick that up on first read. I will try to get better – Com Apr 19 '16 at 17:54
0

In the question you use the example:

- (instancetype)initWithObjects:(ObjectType)firstObj

(this should have been

- (instancetype)initWithObjects:(ObjectType)firstObj, ...

- the rest is in the documentation but due to poor layout is on the next line)

and in a subsequent comment you write:

So NSArray is the instancetype, but why do they put it in ()? initWithObjects is the method. You pass an ObjectType into it. That's the sort of thing I'm trying to understand fully, how to explain it to myself as I go along

It seems you are not yet familiar with what a method declaration looks like in Objective-C, you should read through Apple's Method Declarations Indicate the Messages an Object Can Receive which explains the syntax.

Once you understand how to declare your own methods then reading the ones in the documentation should be easy.

However you have picked an example with a number of more advanced features:

  • instancetype - not really advanced but this is a relatively new way to declare the return type of an initialisation or factory method. See Apple's Adopting Modern Objective-C for an explanation, or just use it for all your initialisation or factory methods and learn why latter.

  • ObjectType - this is related to Apple's new Lightweight Generics. This is a lightly specified feature designed for interacting with Swift (see Interacting with Objective-C APIs). Generally when reading the documentation if you see ObjectType you can replace it with id - meaning "any object type" - to understand the method.

  • firstObj, ... - this is Objective-C variadic parameter syntax - where a method takes a variable number of parameters. The parameter description in the documentation will give more details, which in this case is "A comma-separated list of objects ending with nil". See Apple's Variable arguments in Objective-C methods for more details on variable arguments.

That's it, in one example you've probably hit most of the advanced features you're likely to see.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86