从NSString转换到NSData(On conversion from NSString to NSData)
假设我有一些字符串:
NSString *someString = @"123";
然后,我将这个字符串转换为NSData的实例,如下所示:
NSData *someData = [NSData dataWithBytes:[someString UTF8String] length:[someString length]];
据我所知,NSData实质上只是一个无编码位数据流。 我的问题是:是否NSData通过检查长度参数的值来确定每个UTF8String中有多少位? 换句话说,[someString UTF8String]返回包含字符“123”的C字符串,[someString length]返回整数3. NSData是否理解每个字符必须包含8位? 我完全错过了这一点吗?
谢谢。
Let's say I have some string as such:
NSString *someString = @"123";
Then, I convert this string to an instance of NSData as such:
NSData *someData = [NSData dataWithBytes:[someString UTF8String] length:[someString length]];
As far as I understand, NSData is essentially just an encoding-less stream of bits. My question is: does NSData determine how many bits are in each UTF8String by checking the value for the length parameter? In other words, [someString UTF8String] returns a C string containing the characters "123", and [someString length] returns the integer 3. Does NSData understand that each character then must consist of 8 bits? Am I totally missing the point?
Thank you.
原文:https://stackoverflow.com/questions/19853482
最满意答案
您必须记住在复制手柄时复制计数器。 您可能不希望将操作系统类型传递到模板中,但我认为安全性需要继承。 (但不是从
HANDLE
继承。)
HANDLE
也可能是特殊情况,因为它是POD。 基本上你有一个除T*
之外的类型指针。当计数变为零时,我看到除了
delete
之外你想要的东西的动机。 改编smart_ptr
可能会起作用,而你可能并没有那么远。You have to remember to copy the counter when copying the handle. You might prefer not to pass operating system types into templates, but I think safety here requires inheritance. (Not inheritance from
HANDLE
, though.)
HANDLE
might also be something of a special case because it's POD. Essentially you have a pointer of type besidesT*
.I see the motivation that you want something besides
delete
to happen when the count goes to zero. An adaptation ofsmart_ptr
would probably work, and you might not be that far from just that.
相关问答
更多-
布莱恩 ·艾布拉姆斯(Brad Abrams)在.Net框架的开发过程中发布了一封来自Brian Harry的电子邮件 。 它详细说明了引用计数未被使用的许多原因,即使早期的优先事项之一是保持与使用引用计数的VB6的语义等同。 它考察了一些可能性,例如某些类型引用计数而不是其他类型( IRefCounted !),或者具体的实例被计数,为什么这些解决方案都不被认为是可接受的。 因为[资源管理和决定性决定的问题]是一个敏感的话题,我将尽可能地在我的解释中尽可能准确和完整。 我为邮件的长度道歉。 这封邮件的前9 ...
-
这很容易。 举个例子,假设你有C的fopen接口: FILE* fopen(blah); int fclose(FILE*); 然后你可以像这样使用shared_ptr来包装FILE资源: shared_ptr
ptr(fopen("file.txt", "rt"), fclose); // use the file pointed by ptr fwrite(..., ptr.get()); // FILE is automatically closed by a call to fclo ... -
如果您正在开发MacOSX,那么您可以使用NSMapTable 。 请参阅Mike Ash的博客文章 ,了解更多信息。 如果您正在为iPhone开发,那么您可以使用带有自定义回调结构的CFMutableDictionary : 将kCFTypeDictionaryKeyCallBacks和kCFTypeDictionaryValueCallBacks结构复制到新的结构中。 在不需要保留/释放(键和/或值)的情况下,将retain字段和release字段置于NULL。 调用CFDictionaryCreate ...
-
跨AppDomains实施引用计数的最佳方法是什么?(What would be the best way to implement reference counting across AppDomains?)[2023-10-01]
几年前,我写了一篇关于解决类似问题的博客文章。 http://netvignettes.wordpress.com/2012/01/03/sharepoint-net-3-5-woes-and-synchronization-between-processes/ 向下滚动到代码块并开始在那里阅读,这样您就可以跳过顶部的SharePoint cruft。 I wrote a blog post about a solution to a similar problem a couple of years ag ... -
您必须记住在复制手柄时复制计数器。 您可能不希望将操作系统类型传递到模板中,但我认为安全性需要继承。 (但不是从HANDLE继承。) HANDLE也可能是特殊情况,因为它是POD。 基本上你有一个除T*之外的类型指针。 当计数变为零时,我看到除了delete之外你想要的东西的动机。 改编smart_ptr可能会起作用,而你可能并没有那么远。 You have to remember to copy the counter when copying the handle. You might prefer n ...
-
Flanfl的回答是不正确的。 iOS 4 确实支持ARC ,但不支持自动弱引用归零。 也被称为“ARClite”。 请参阅Apple关于“ Objective-C功能可用性索引 ”的文档。 ARC需要Xcode 4.2或更高版本。 ARC部署回iOS 4。 在StackOverflow.com上查看另一个问题: iOS 4中是否真的支持ARC? iOS 4.2 SDK在链接时缺少与ARC相关的符号 顺便说一句, 版本4.2.1是iOS 4的最新版本,可在所有不支持iOS 5和6的设备上运行。具体来说,在i ...
-
检查了CAutoPtr源后,不支持引用计数。 如果需要此功能,请使用boost :: shared_ptr。 Having checked the CAutoPtr source, no, reference counting is not supported. Using boost::shared_ptr instead if this ability is required.
-
Java中的引用计数(Reference counting in Java)[2023-07-05]
不要依赖垃圾收集器。 它被故意设计为不可靠。 如果“共享”意味着你在你的代码中使用了它的几个地方,所以你不能关闭它,我建议你改变你的代码以拥有一个集中的文件池,在那里你可以“检出”一个文件句柄以用于你的代码在本地。 close()过程然后将文件句柄返回到池。 跟踪您的句柄,当给定文件的所有句柄都返回到池时,关闭文件。 Do not depend on the garbage collector. It is intentionally designed not to be dependable. If "s ... -
Kids *one; 似乎是未初始化的。 将值复制到它时。 这个值也是单元化的,因为它是私有的,我没有看到任何init代码。 你必须添加类似的东西 kids(new Kids()) 在Person构造函数中,不是复制一个。 PS。 不要忘记operator =和析构函数。 Kids *one; Seems to be uninitialized. When you copy a value to it. this value is also unitialized, since it is priva ...
-
可以使用引用计数,例如,关闭未引用的文件句柄,或以某种方式“存档”当前未引用的数据(可能在将来通过某些间接路径重新引用)。 Reference counting may be used, eg, to close unreferenced file handles, or to somehow "archive" currently unreferenced data (that can potentially be rereferenced through some indirect path in th ...