Char * vs NSString *

I am trying to get rid of some confusion when working with C.

Uses char *name in a sense, the same thing that works with NSString *name in the sense that the pointer still points to the first memory allocation for it?

Ofcourse NSString has a bunch of extra features, but that’s not what I mean, it will work with char *name so that it works as if it were NSString *name , so I could just work with the "name" pointer in the future ?

+4
source share
5 answers

The answer is no.

char* means to specify a simple array of char values ​​(or single).

 char* myCharPtr = "This is a string."; //In memory: myCharPtr contains an address, eg |0x27648164| //at address 0x27648164: |T|h|i|s| |i|s| |a| |s|t|r|i|n|g|.|\0| 

On the other hand, NSString *name will be a pointer to an object that can have many additional functions, and you cannot rely on where the actual symbol data is stored, or how. It is not encoded as ASCII (Sherm Pendley below, says UTF-16), and it may have additional data such as string length, etc.

 NSString* myStringPtr = @"This is an NSString."; //In memory: myStringPtr contains eg |0x27648164| //at address 0x27648164: |Object data|length|You don't know what else|UTF-16 data|etc.| 

You modify and access unknown objects through their open methods, because you do not know how they are formatted in memory and, therefore, cannot directly access their data. (Or because they encapsulate themselves to hide it from you.)

You can use the NSString pointer later if you declare it as NSString *name . Here is what I mean:

 NSString *name = @"This is a string."; NSString *sameName = name; //This is pointing to the same object as name NSLog(@"%@", sameName); //This prints "This is a string.", and if NSStrings were mutable, changing it would change name as well. //They point to the same object. 
+19
source

char* and NSString* are two completely different string implementations.

NSString can only be used in Objective-C (not in plain C), represents immutable strings, is based on Unicode characters, and - being an object-oriented class - provides many methods. In addition, they are counted and always distributed on the heap.

char* is just any byte array whose encoding is undefined, modified, and not object oriented. char arrays can be allocated on the heap (using malloc ) or on the stack. The former requires calling free , and the latter should never be freed.

+4
source

Uses the char * name in a way, like working with the name NSString * in the sense that the pointer still points to the first memory allocation for this?

I think you should consider the name NSString * as an object descriptor, not a pointer to an object, and therefore do not assume that it points to the first memory allocation. I think that would be the best metaphor.

+2
source

No,

char * is a pointer to an array of characters. NSString is a pointer to an NSString object. I cannot find documentation about this, but I believe that NSString puts a char *. Executing the name [1] with respect to NSString * name will refer to the second element of the array of NSString objects.

0
source

In a sense, this is somewhat impracticable - they are the same, since both are types of pointers that reference a memory address. However, this similarity is not very useful in practice. To use one of them, you must consider the type of β€œthing” that they point to, and how one uses the char array and the NSString instance is completely different. Even for something as simple as assignment, you need to process an NSString instance in accordance with Cocoa's memory management rules, using either -copy or -retain instead of simply assigning "foo = bar".

0
source

Source: https://habr.com/ru/post/1345342/


All Articles