0

I am pulling in the components from a NSURL using componentswithurl: resolvingagainstbaseurl:. This gives me an NSArray of query items, but I cannot get the value for each object out of the array based on the key. I am doing this for a custom url that is launching my app from another app. I am doing something like:

Incoming url: something://init?item1=xxxx&item2=yyy&item3=www2&item4=sss

NSURLComponents *comp = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:true];
NSAray *compDict = comp.queryItems;

Thus giving me a list of object with a key value pair.

compDict[0] =
    name = item1
    value = xxxx

and so on. I am trying to get the value based on the name(key) using:

NSString *value = [compDict valueForKey:@"item1"];

but this gives me an error - [<NSURLQueryItem> valueforundefinedkey:]this class is not key value coding-compliant for key item1.

Can someone help me out on this.

Willeke
  • 14,578
  • 4
  • 19
  • 47
D. Rock
  • 1
  • 3
  • `compDict` is not a `Dictionary`, it's an array of `NSURLQueryItem`. Iterate on it, and get its `name` and `value` property: for `(NSULRQueryItem *aQueryItetm in compDict) { NSLog(@"Key: %@ - Value: %@", [aQueryItem key], [aQueryItem value]); }` – Larme Apr 19 '22 at 15:52
  • [aQueryItem key] terminated with an uncaught exception. 'NSInvalidArgumentException' , reason: '-[NSURLQueryItem key]: unrecognized selector sent to instance. – D. Rock Apr 19 '22 at 16:07
  • Sorry, it's `[aQueryItem name]`, since the property in `NSURLQueryItem` is `name`, not `key`: https://developer.apple.com/documentation/foundation/nsurlqueryitem?language=objc – Larme Apr 19 '22 at 16:08
  • Excellent. Name fixed the issue. Thank you. – D. Rock Apr 19 '22 at 16:18

0 Answers0