What Does the Ns Prefix Mean

What does the NS prefix mean?

It's from the NeXTSTEP heritage.

What's the meaning of prefix _ in parameters in Objective-C

When you use

@synthesize param2 = _param2;

means that you are using a different name to access the instance variable directly.

(In new XCode versions, if you dont specify the synthesize yourself XCode writes one for you, same as that one)

If you use:

_param2

You are accessing the instance variable directly.

If you use:

self.param2

You are accessing the variable trough the setter/getter, and those setter/getter are defined using the properties you set.

As a rule of thumb, you want to access you ivar directly in the init methods, and in the rest of the class you use self.

If you want to get more info on this, just follow this link:

Encapsulating data in Objective-C

What does the prefix in NSLog mean?

21856 is the process id. 71939 is the thread id.

You can generate this portion of the log on your own using:

[NSString stringWithFormat:@"[%ld,%lx]",
(long) getpid(),
(long) pthread_mach_thread_np(pthread_self())];

Edit 2014-09-23:

At least on the simulator in iOS 8, the second number is now the pthread_threadid_np of the thread.

    __uint64_t threadId;
if (pthread_threadid_np(0, &threadId)) {
threadId = pthread_mach_thread_np(pthread_self());
}

[NSString stringWithFormat:@"[%ld,%llu]", (long) getpid(), threadId]

What does the kCF prefix mean?

In both OS X Frameworks and
iOS Frameworks
you find a list of system frameworks together with the key prefixes used by the classes, methods, functions, types, or constants of the framework.

This list contains


Name Prefix
--------------------------------
...
CFNetwork.framework CF
...
CoreFoundation.framework CF
...

So there are actually several frameworks using the "CF" prefix, but
Core Foundation is the most "prominent" one.

The "k" prefix for constants is documented in
Naming Conventions
in "Introduction to Core Foundation Design Concepts"
(emphasis added):

A major programming-interface convention in Core Foundation is to use
the name of the opaque type that is most closely related to a symbol
as the symbol’s prefix. For functions, this prefix identifies not only
the type to which the function “belongs” but usually the type of
object that is the target of the function’s action. (An exception to
this convention are constants, which put “k” before the type prefix.)

As an example, kCFAllocatorMalloc is a constant related to
the CFAllocator type which is part of the Core Foundation framework.

Eclipse's XML Editor Includes ns: Prefix

For some reason, the file had the schema listed twice, once with a namespace prefix. Removed the second one, works now.

<book xmlns="http://www.manning.com/schemas/book" xmlns:ns="http://www.manning.com/schemas/book"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Thanks.

CF objects vs NS objects

To answer your questions in order:

  1. What's the point of them both existing? There are a few reasons.

    If you want to provide a C API, like the Carbon API, and you need things like arrays and dictionaries of referenced-counted objects, you want a library like Core Foundation (which provides CFArray), and of course it needs to have a C API.

    If you want to write libraries for third-parties to use on Windows (for example), you need to provide a C API.

    If you want to write a low-level library, say for interfacing with your operating system's kernel, and you don't want the overhead of Objective-C messaging, you need a C API.

    So those are good reasons for having Core Foundation, a pure C library.

    But if you want to provide a higher-level, more pleasant API in Objective-C, you want Objective-C objects that represent arrays, dictionaries, reference-counted objects, and so on. So you need Foundation, which is an Objective-C library.

  2. When should you use one or the other? Generally, you should use the Objective-C classes (e.g. NSArray) whenever you can, because the Objective-C interface is more pleasant to use: myArray.count (or [myArray count]) is easier to read and write than CFArrayGetCount(myArray). You should use the Core Foundation API only when you really need to: when you're on a platform that doesn't have Objective-C, or when you need features that the Core Foundation API provides but the Objective-C objects lack. For example, you can specify callbacks when creating a CFArray or a CFDictionary that let you store non-reference-counted objects. The NSArray and NSDictionary classes don't let you do that - they always assume you are storing reference-counted objects.

  3. Are the CF objects just legacy objects? Not at all. In fact, Nextstep existed for years with just the Objective-C Foundation library and no (public) Core Foundation library. When Apple needed to support both the Carbon API and the Cocoa API on top of the same lower-level operating system facilities, they created (or made public) Core Foundation to support both.

Incidentally, some of Core Foundation is open source. You can find the open source part of it for Mac OS X 10.10.5 here: https://opensource.apple.com/source/CF/CF-1153.18/. I have found the source code of CFRunLoop and CFStream to be very informative.

Is prefix necessary for methods in iOS?

For Classes which contains project related storylines/stuffs, there prefixes are not required.

But if we are using Apple Classes by extending few methods as given in example, like UIView to MBView then we should add prefix to methods to private methods in private category ( in .m file).



Related Topics



Leave a reply



Submit