iOS无法将字典值获取到数组
<p><p>我有字典:</p>
<pre><code>{
6 = (
{
id = 6;
name = Andrea;
},
{
id = 6;
name = Paolo;
}
);
8 = (
{
id = 8;
name = Maria;
}
);
67 = (
{
id = 67;
name = Francesco;
},
{
id = 67;
name = Sara;
}
);
}
</code></pre>
<p>我试图将值放入数组中。</p>
<p>我的代码是:</p>
<pre><code> NSArray *arrayNew= ;
NSLog(@"Hello:%@",arrayNew);
</code></pre>
<p>但我总是得到空值。</p>
<p>我的完整代码:</p>
<pre><code> NSMutableArray *idArray=[init];
NSString* path = [ pathForResource:@"File"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableData *results11 = ;
NSLog(@"Check:%@",results11);
NSArray *array= ;
NSMutableDictionary* result = ;
for (NSDictionary* dict in array)
{
NSNumber* anID = ;
if (!]) {
// do something
];
}
NSMutableArray* resultsForID = ;
if (!resultsForID)
{
resultsForID = ;
;
}
;
}
NSLog(@"Result:%@",result);
NSLog(@"ID arr:%@",idArray);
// NSArray *temp= ;
NSString *arrayNew= ;
NSLog(@"Hello:%@",arrayNew);
</code></pre>
<p>文件.txt : <code>{
“ field ”: [
{
“身份证”:6,
“名称”:“安德烈亚”
},
{
“身份证”:67,
“名称”:“弗朗西斯科”
},
{
“身份证”:8,
“名字”:“玛丽亚”
},
{
“身份证”:6,
“名称”:“保罗”
},
{
“身份证”:67,
“名称”:“萨拉”
}
]
}</code></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>最后引用 Tommy 的解决方案,我解决了我的问题。</p>
<pre><code>NSArray *commentArray = result[@67];
</code></pre>
<p>还有我的最终代码:</p>
<pre><code> NSMutableArray *idArray=[init];
NSString* path = [ pathForResource:@"File"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableData *results11 = ;
NSLog(@"Check:%@",results11);
NSArray *array= ;
NSMutableDictionary* result = ;
for (NSDictionary* dict in array)
{
NSNumber* anID = ;
if (!]) {
// do something
];
}
NSMutableArray* resultsForID = ;
if (!resultsForID)
{
resultsForID = ;
;
}
;
}
NSLog(@"Result:%@",result);
NSMutableArray *arrayNew = [ init];
for (int i=0; i<; i++) {
NSArray *commentArray = result];
NSLog(@"COMM:%@",commentArray);
NSMutableArray *arr=[init];
for (NSDictionary* dict in commentArray)
{
];
}
;
}
NSLog(@"ID arr:%@",idArray);
NSLog(@"Name arr:%@",arrayNew);
</code></pre>
<p><strong>文件.txt:</strong></p>
<pre><code>{
"field": [
{
"id": 6,
"name": "Andrea"
},
{
"id": 67,
"name": "Francesco"
},
{
"id": 8,
"name": "Maria"
},
{
"id": 6,
"name": "Paolo"
},
{
"id": 67,
"name": "Sara"
}
]
}
</code></pre>
<p>我的最终结果:</p>
<pre><code>ID arr:(
6,
67,
8
)
Name arr:(
(
Andrea,
Paolo
),
(
Francesco,
Sara
),
(
Maria
)
)
</code></pre></p>
<p style="font-size: 20px;">关于iOS无法将字典值获取到数组,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28358787/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28358787/
</a>
</p>
页:
[1]