Re: Can't cram an NSRange into an NSArray?
Re: Can't cram an NSRange into an NSArray?
- Subject: Re: Can't cram an NSRange into an NSArray?
- From: Bill Bumgarner <email@hidden>
- Date: Wed, 19 Jun 2002 16:31:50 -0400
As noted, NS[Mutable]Arrays are designed to hold Objective-C objects, not
arbitrary data and not pointers. To repeat...
NSRange aRange = NSMakeRange(0, 100);
[anArray addObject: aRange];
[anArray addObject: &aRange];
... will *not* work.
Using NSStringFromRange() and NSRangeFromString() will work, as noted, but
is inefficient in that it requires parsing and unparsing the NSRange
structure.
NSValue offers several bits of API that can be used to store arbitrary
hunks of data. Assuming you don't want to use malloc() and free() to
manage hunks of memory by hand, the following will work:
NSRange myRange = NSMakeRange(4, 10);
NSValue *theValue = [NSValue valueWithBytes:&myRange
objCType:@encode(NSRange)];
[anArray addObject: theValue];
The range can be retrieved via:
NSRange myRange;
NSValue *theValue = [anArray objectAtIndex: 0];
[theValue getValue: &myRange];
For a full explanation, see the documentation for NSValue or the Concepts
guide for NSValue.
file:///Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/NumbersandValues/
Concepts/Values.html
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.