Here's what I want to do:
NSRange r = NSMakeRange(0,5); id a = [NSMutableArray a]; [a addObject: r]; // but NSRange is not a NSObject *
With a boolean, I'd use code like this:
[a addObject: [NSNumber numberWithBool: YES]];
or with an integer:
[a addObject: [NSNumber numberWithInteger: 3]];
So what's the equivalent with a NSRange
? What I don't really want to do is create my own subclass of NSObject
to accomplish this. Surely there's a way with what Apple's already provided?
The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .
NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef . See Toll-Free Bridging for more information on toll-free bridging.
The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...
Use NSValue's +valueWithRange:
. To retrieve the range structure back, use the property rangeValue
.
[a addObject:[NSValue valueWithRange:r]]; ... NSRange r = a[4].rangeValue;
[NSValue valueWithRange:r];
and get it back out with:
NSRange r = [rangeObject rangeValue];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With