Task C- defines in C or object C

I am a beginner, so please bear with me. First of all, is this all NS stuff (NSArray, NSString, etc.) objective-C specific?

Also, I am confused about creating things in C or objective-C. When do you use one or the other?

For example, which cases I would use below:

NSArray *germanCars = @[@"Mercedes-Benz", @"BMW", @"Porsche", @"Opel", @"Volkswagen", @"Audi"];

      

or

NSString *germanCars[] = {@"Mercedes-Benz", @"BMW", @"Porsche", @"Opel", @"Volkswagen", @"Audi"};

      

thank

+3


source to share


3 answers


The NS material comes from the NeXTSTEP operating system: http://en.wikipedia.org/wiki/NeXTSTEP

Steve Jobs launched NeXT and brought things to Apple when he returned there.

When working with Mac OS or iOS, this is part of the Cocoa framework. Objective-C is just the language these things are written in.



As for your coding question, use the first one. You want to use Cocoa's constructors as much as possible. Cocoa is a mature, well thought out API, and you will find many tasks easier if you do things with this style.

+2


source


The prefixed stuff is NS

specific to Cocoa, Foundation and AppKit frames. They were originally intended for Objective-C, but they are available from other languages ​​as long as they have a binding. The obvious one these days is Swift, but they can also be accessed from Python using PyObjC etc.



In general, you should prefer Cocoa collection classes (i.e. NSArray

) over C-style types. They are of a higher level and thus provide better abstractions and functionality. You would only use a C-style array for APIs that require it, which are relatively rare.

+3


source


Your first example is an NSArray object containing multiple NSString objects. The second example is a regular C array containing multiple NSString objects.

In the Cocoa library, you will find very few methods that accept C arrays of objects. And NSArray has a huge set of useful functions that C arrays don't have.

+1


source







All Articles