gpt4 book ai didi

ios - 如何从数组中获取一个特定键的所有值?

转载 作者:行者123 更新时间:2023-11-28 18:28:54 26 4
gpt4 key购买 nike

我正在使用 Objective C 开发应用程序。

我在服务器端的响应中得到以下数组(我正在使用 AFNetworkingGet 方法)。

 Response from server: `[{"name":"option1","profile_id":0,"profile_name":"option1"},{"name":"option2","profile_id":0,"profile_name":"option2"},{"name":"option3","profile_id":0,"profile_name":"option3"},{"name":"option4","profile_id":0,"profile_name":"option4"},{"name":"option5","profile_id":0,"profile_name":"option5"},{"name":"option6","profile_id":0,"profile_name":"option6"},{"name":"option7","profile_id":0,"profile_name":"option7"},{"name":"option8","profile_id":0,"profile_name":"option8"},{"name":"option9","profile_id":0,"profile_name":"option9"},{"name":"option10","profile_id":0,"profile_name":"option10"}]`

我想分隔键 name 的值。

简而言之,我想要这样的输出:[option1,option2,option3,option4,option5,option6,option7,option8,option9,option10]

为此,我尝试了以下方式:

将该响应存储在数组中 (_arr)。然后尝试像下面这样分离键 name 的值,

   NSArray *optionArray = [_arr valueForKey:@"name"]; 
NSLog(@"%@",optionArray);

但是,不幸的是,这是行不通的。应用程序崩溃并提供以下日志。

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7abd0600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x00ae2a14 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x005a3e02 objc_exception_throw + 50
2 CoreFoundation 0x00ae2631 -[NSException raise] + 17
3 Foundation 0x00239098 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x0017a798 _NSGetUsingKeyValueGetter + 105
5 Foundation 0x0017a727 -[NSObject(NSKeyValueCoding) valueForKey:] + 288
6 Foundation 0x001bac40 -[NSFunctionExpression expressionValueWithObject:context:] + 1079
7 Foundation 0x001ba739 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 290
8 Foundation 0x001ba60f -[NSPredicate evaluateWithObject:] + 48
9 Foundation 0x001ba593 _filterObjectsUsingPredicate + 437
10 Foundation 0x001ba360 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 314
11 Wellness_24x7 0x0008fbc4 __31-[ForthViewController Donating]_block_invoke + 564
12 Wellness_24x7 0x0006c097 __116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke97 + 231
13 Wellness_24x7 0x00080bd5 __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2132 + 213
14 libdispatch.dylib 0x0330e377 _dispatch_call_block_and_release + 15
15 libdispatch.dylib 0x033319cd _dispatch_client_callout + 14
16 libdispatch.dylib 0x03316f90 _dispatch_main_queue_callback_4CF + 910
17 CoreFoundation 0x00a33fde __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
18 CoreFoundation 0x009f1cd4 __CFRunLoopRun + 2356
19 CoreFoundation 0x009f10e6 CFRunLoopRunSpecific + 470
20 CoreFoundation 0x009f0efb CFRunLoopRunInMode + 123
21 GraphicsServices 0x050c8664 GSEventRunModal + 192
22 GraphicsServices 0x050c84a1 GSEventRun + 104
23 UIKit 0x012ecbfa UIApplicationMain + 160
24 Wellness_24x7 0x0006905a main + 138
25 libdyld.dylib 0x0335ba21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

提前谢谢你。

最佳答案

实际上您的 JSON 响应以 Array 开头,所以喜欢

NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: data options: KNilOptions error: nil];

NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in ResponseArray) {
[finalArray addObject:temp[@"name"]];
}

AFNetworking 的选择 2

   NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in responseObject) {
[finalArray addObject:temp[@"name"]];
}

关于ios - 如何从数组中获取一个特定键的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40761623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com