首页 \ 问答 \ 从NSString转换到NSData(On conversion from NSString to NSData)

从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
更新时间:2022-07-29 21:07

最满意答案

您必须记住在复制手柄时复制计数器。 您可能不希望将操作系统类型传递到模板中,但我认为安全性需要继承。 (但不是从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 besides T*.

I see the motivation that you want something besides delete to happen when the count goes to zero. An adaptation of smart_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 ...
  • 几年前,我写了一篇关于解决类似问题的博客文章。 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.
  • 不要依赖垃圾收集器。 它被故意设计为不可靠。 如果“共享”意味着你在你的代码中使用了它的几个地方,所以你不能关闭它,我建议你改变你的代码以拥有一个集中的文件池,在那里你可以“检出”一个文件句柄以用于你的代码在本地。 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)