Question

I need to store both the NSRange object and NSInteger inside the NSMutableDictionary?

Can I do it?

If it is, Could you give me an example?

Was it helpful?

Solution

You can store the NSInteger as NSNumber object.

mutableDictionary[integerKey] = [NSNumber numberWithInteger:integer];

or even better

mutableDictionary[integerKey] = @(integer);

For NSRange, use NSValue object.

mutableDictionary[rangeKey] = [NSValue valueWithRange:range];

OTHER TIPS

I have used NSString for both NSRange and NSInteger, to remove confusion use this:

NSInteger i = 6;
    NSRange range = NSMakeRange (25, 3);
    NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
    [dic setObject:NSStringFromRange(range) forKey:@"range"];
    [dic setObject:[NSString stringWithFormat:@"%d",i] forKey:@"integer"];
    //How to get it...
    NSRange range1 =  NSRangeFromString([dic objectForKey:@"range"]) ;
    NSInteger inte = [[dic objectForKey:@"integer"] integerValue];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top