1

I am trying to parse a URL query component to an unsigned integer. When trying to cast the string extracted from the URL (an NSTaggedPointerString), to unsigned integer, the program crashes with

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString unsignedIntegerValue]: unrecognized selector sent to instance XXX

My application:openURL:options: method in my AppDelegate is below:

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];

    NSMutableDictionary *queryItemDict = [NSMutableDictionary dictionaryWithCapacity:components.queryItems.count];

    for (NSURLQueryItem *queryItem in components.queryItems) {
        [queryItemDict setObject:queryItem.value forKey:queryItem.name];
    }

    if ([[queryItemDict allKeys] containsObject:@"selectedSection"]) {
        NSUInteger selectedSection = 0;
        NSLog(@"%@",[[queryItemDict objectForKey:@"selectedSection"] class]);
        selectedSection = [[queryItemDict objectForKey:@"selectedSection"] unsignedIntegerValue];
    }


    return YES;
}

I have tested this in a blank iOS Xcode project and registered the URL scheme "test" like so register URL ios

My test url is test://selectedSection=0.

I can upload the sample project if needed, but the above two steps are all that is needed to reproduce. Am I not casting the NSString to unsigned integer correctly?

ansonl
  • 786
  • 1
  • 13
  • 34
  • 2
    Have you looked at the documentation for `NSString`? There is no `unsignedIntegerValue` method. – rmaddy Mar 31 '17 at 03:27
  • Thanks. I was able to cast the positive `NSInteger` to `NSUInteger`. `selectedSection = (NSUInteger)[[queryItemDict objectForKey:@"selectedSection"] integerValue];` – ansonl Apr 01 '17 at 03:48

0 Answers0