Confused about the size and type of `NSString` in Objective-C? Learn why `lengthOfBytesUsingEncoding` returns 1 byte and `sizeof` returns 8 bytes, and what that means for your programming journey!
---
This video is based on the question https://stackoverflow.com/q/69696151/ asked by the user 'stefanosn' ( https://stackoverflow.com/u/235532/ ) and on the answer https://stackoverflow.com/a/69696283/ provided by the user 'Sulthan' ( https://stackoverflow.com/u/669586/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: What type is NSString and how many bytes?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Size of NSString: A Beginner's Guide to Objective-C
As newcomers to Objective-C often encounter, the nature of NSString can be perplexing, especially when it comes to understanding its size and type. Today's guide aims to demystify these concepts for you. We'll explore a common question: What type is NSString, and how many bytes does it take up in memory?
The Problem at Hand
Imagine you have the following piece of code:
[[See Video to Reveal this Text or Code Snippet]]
When you run this code, you receive the output:
LengthOfBytesUsingEncoding: 1 byte
NSString: 8 bytes
This leads to a natural confusion: Why does lengthOfBytesUsingEncoding return 1 byte while sizeof outputs 8 bytes? Let's clarify this.
Understanding the Results
1. lengthOfBytesUsingEncoding
The lengthOfBytesUsingEncoding method tells you the number of bytes required to represent the string in a specific encoding. In this case, your string is "a", containing a single character, which in UTF-8 encoding requires 1 byte.
So, if you're wondering about what that number signifies:
1 byte = the size of the string content stored in memory as UTF-8.
2. sizeof
On the other hand, the sizeof operator gives you the size of the variable itself, which is actually a pointer to an NSString object. In this case:
8 bytes = the size of a pointer on a 64-bit architecture system.
Understanding Pointers
In programming, a pointer is a variable that holds the memory address of another variable. For Objective-C:
The NSString data you're dealing with exists somewhere in memory; what your variable holds is merely a reference to that location.
Because you're using a pointer, its size is determined by the architecture you're compiling for; hence, on a 64-bit system, it's consistently 8 bytes.
Recap: Key Takeaways
To sum it up, here’s what you need to remember:
lengthOfBytesUsingEncoding calculates the byte length of the actual string content. It's focused purely on the data, which, in the case of "a", is a single byte.
sizeof reveals the size of the pointer that references the NSString object. In a 64-bit environment, this pointer is always 8 bytes.
The actual structure of NSString in memory is more complex, and its actual size may differ based on its subclass (like NSMutableString or others).
Important Note
As you delve deeper into Objective-C, you'll find that while sizeof can provide insight into variable sizes, it’s not often critical to your day-to-day coding tasks, especially if you're not performing pointer arithmetic.
Final Thoughts
Understanding data types and memory management is vital in Objective-C, particularly when dealing with string objects. By grasping the difference between byte size and pointer size, you equip yourself with foundational knowledge that will help as you dive deeper into programming with Objective-C.
If you have any more questions or would like to explore other concepts about Objective-C, feel free to leave your thoughts in the comments below!
Информация по комментариям в разработке