Jak przechowywać NSRange w kontenerze NSMutableArray lub innym?

Oto co chcę zrobić:

NSRange r = NSMakeRange(0,5);
id a = [NSMutableArray a];
[a addObject: r]; // but NSRange is not a NSObject *

Z boolean, użyłbym kodu takiego:

[a addObject: [NSNumber numberWithBool: YES]];

Lub z liczbą całkowitą:

[a addObject: [NSNumber numberWithInteger: 3]];

Więc jaki jest odpowiednik NSRange? Nie chcę tworzyć własnej podklasy NSObject, Aby to osiągnąć. Na pewno jest sposób na to, co Apple już dostarczyło?

Author: Steven Fisher, 2010-10-22

4 answers

Użyj NSValue ' s +valueWithRange:. Aby odzyskać strukturę zakresu, użyj właściwości rangeValue.

[a addObject:[NSValue valueWithRange:r]];
...

NSRange r = a[4].rangeValue;
 132
Author: kennytm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-11 11:47:08
[NSValue valueWithRange:r];

I odzyskaj to z:

NSRange r = [rangeObject rangeValue];
 11
Author: mipadi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-22 17:05:35

Jeśli chcesz zapisać NSRange na liście właściwości, Możesz również przekształcić {[0] } w NSString za pomocą funkcji NSStringFromRange. Następnie możesz przekształcić ten ciąg z powrotem w zakres za pomocą funkcji NSRangeFromString.

 5
Author: James Huddleston,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-22 17:06:28

Inną opcją może być dodanie tych zakresów do NSIndexSet, w zależności od tego, w jaki sposób zamierzasz ich użyć.

 4
Author: Mike Abdullah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-22 23:25:41