访问封闭/父对象的NSDictionary(Access to enclosing/parent NSDictionary of an object)
有什么方法可以发送给一个对象,它会返回一个封闭集合(NSDictionary / NSArray)? 还是一种方法,它将返回所有对象的集合,并在有问题的对象上保留一个调用?
DJS。
@bburn:这可能是一个设计问题。 我试图解析一个XML文档,并且或多或少地将整个xml树转换为一个嵌套的NSDictionary树。 我正在构建一个类,我将在不同的项目中多次使用这个类,但是XML源始终是相同的,因此我想保存自己一些重复的工作,并且吐出一个可可“本机”数据结构,而不是NSXML / Document对象。
我认为我需要了解父母,以帮助获取我在解析过程中的位置。 目前使用带有委托对象的NSXMLParser。
Is there any method I can send to an object, which will return an enclosing collection (NSDictionary/NSArray)? Or a method which would return a collection of all the objects with a retain call on the object in question?
DJS.
@bburn: It possibly is a design problem. I'm trying to parse an XML document, and more or less convert the entire xml tree into a nested NSDictionary tree. I am building a class which I will use many times over in different projects, but the XML source is always the same, thus I want to save myself some repeated work and have it spit out a cocoa "native" data structure, rather than an NSXML/Document object.
I thought I needed to know the parent to help gain context of where I am in the parsing process. Currently using NSXMLParser with a delegate object.
原文:https://stackoverflow.com/questions/1957806
最满意答案
这是一个基于你当前代码的重构。
public override string ToString() { var output = new StringBuilder(); output.AppendFormat("{0}", count); var index = 0; foreach (var item in array) { if (item!=null) { output.AppendFormat("{0}{1}{2} ", item, item.GetInfo, string.Join(" ", item.Content(index++)), ); } } return output.ToString(); }
使用linq你可以执行相同的索引
Select
而不直接调用在引擎盖下使用的foreachpublic override string ToString() { var output = new StringBuilder(); output.AppendFormat("{0}", count); array.Select((item, index) => output.AppendFormat("{0}{1}{2} ", item, item.GetInfo, string.Join(" ", item.Content(index)), ) ); return output.ToString(); }
Here is a refactoring based on your current code.
public override string ToString() { var output = new StringBuilder(); output.AppendFormat("{0}", count); var index = 0; foreach (var item in array) { if (item!=null) { output.AppendFormat("{0}{1}{2} ", item, item.GetInfo, string.Join(" ", item.Content(index++)), ); } } return output.ToString(); }
Using linq you could perform the same with a indexed
Select
without directly calling the foreach which is used under the hoodpublic override string ToString() { var output = new StringBuilder(); output.AppendFormat("{0}", count); array.Select((item, index) => output.AppendFormat("{0}{1}{2} ", item, item.GetInfo, string.Join(" ", item.Content(index)), ) ); return output.ToString(); }
相关问答
更多-
如何找到foreach索引(How to find the foreach index)[2023-02-04]
foreach($array as $key=>$value) { // do stuff } $key是每个$array元素的索引 foreach($array as $key=>$value) { // do stuff } $key is the index of each $array element -
foreach是用于迭代实现IEnumerable集合。 它通过在集合上调用GetEnumerator来执行此操作,它将返回Enumerator 。 这个枚举器有一个方法和属性: 的MoveNext() 当前 Current返回Enumerator Current处于的对象, MoveNext Current更新为下一个对象。 显然,一个指数的概念是枚举的概念是外来的,不能做到。 因此,大多数集合都可以使用索引器和for循环构造遍历。 在这种情况下,我更喜欢使用for循环,与使用局部变量跟踪索引。 The ...
-
在foreach中增加索引(Incrementing index in foreach)[2023-12-08]
$x++返回$x++的旧值(在增量之前)。 例如, $x=3; print $x++; $x=3; print $x++; 将打印“3”。 它也修改$x ,所以它不是最好的选择,除非这是你的意图。 (在这里,递增将是无用的,特别是因为修改过的键永远不会看到循环的外部。) ++$x会返回++$x的新值。 但是,就像$x++一样,在获取下一个数字时它在语义上是错误的,因为它修改了$x 。 只要坚持$x + 1 。 $x++ returns the old value of $x (before the incr ... -
即使它可能(通过prev或其他一些hack),以这种方式编写的代码很难理解(即使是在几年内你自己)。 对于程序员来说, foreach构造(乍一看)看起来像是对数组的所有元素的简单迭代,而不是来回跳跃。 另请注意, foreach循环中的prev 可以在一个PHP版本中工作,但在其他版本中失败 ! PHP文档说 : 由于foreach依赖于内部数组指针,因此在循环内更改它可能会导致意外行为 。 我建议在你的情况下使用while循环(使用索引或each - prev - next函数)。 while构造是程序 ...
-
这是一个基于你当前代码的重构。 public override string ToString() { var output = new StringBuilder(); output.AppendFormat("{0}", count); var index = 0; foreach (var item in array) { if (item!=null) { output.AppendFormat("{0}{1}{2} ", it ...
-
JVM是否在Java中跟踪foreach循环中的索引?(Does JVM keeps track of index in foreach loop in Java? If so, How?)[2022-05-05]
依靠。 这个循环有两个版本,分别用于数组和Iterable (比如List )。 对于数组,编译器将为for (int i=0; i有时,“将{x}转换为LINQ”的正确方法是:不要。 它不适合每项工作。 你使用foreach和字典变异的当前代码很好。 收下。 如果重复的id(在Distinct() )不是问题,你可以做: var map = testItems.Distinct().ToDictionary(x => x.GetId()); 但就是这样。 Sometimes the correct approach to "converting {x} to LINQ" is: don't. It isn't an appropri ...如何在knockout中从外部foreach循环访问索引变量(How can I access index variable from outer foreach loop in knockout)[2022-01-23]
基本上要走2级了 $parentContext.$parentContext.$index() 请参阅以下示例https://jsfiddle.net/wgsdddtj/ var viewModel = function () { var self = this; var Product = function (name, products) { var pSelf = this; pSelf.name = ko.observable(name); pSelf ...是的,要么使用for循环(类似于(gc c:\ATextFile.txt).count将是上限)或外部计数器。 相关答案(对于C#,但基本上都使用相同的可枚举概念): 如何获取foreach循环的当前迭代的索引? Yes, either use a for loop (something like (gc c:\ATextFile.txt).count would be the upper limit ) or an external counter. Related answer ( for C#, bu ...你说element3我假设你的意思是element2 for (var i = 0; i < 2; i++) { console.log('i:', i); ["element0", "element1", "element2"].forEach(function (element, index) { console.log('element:', element, 'index:', i); }); } 你使用i作为循环计数器和forEach索引 you said element3 i'm as ...相关文章
更多- Object Oriented Programming
- Guava学习笔记:复写的Object常用方法
- Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
- 不显示删除回复显示所有回复显示星级回复显示得分回复 请问java Object的hashcode()方法是怎么实现的啊,内部的算法是什么啊
- 关于对象复制时出现内容不一致的问题
- Java父类使用this.getClass().getName() 获取到的是子类类名
- Storm【设计细节】 -Fields对象
- nslocal notification
- javascript 对象转换成json格式数据(使用JSON.stringify)
- RFC2848--The PINT Service Protocol: Extensions to SIP and SDP for IP Access to Telephone Call Services
最新问答
更多- 您如何使用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)