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: Andy Lee <email@hidden>
- Date: Wed, 19 Jun 2002 16:10:17 -0400
At 1:42 PM -0600 6/19/02, Carl Jochen Norum wrote:
Quoting James Winetke <email@hidden>:
> With ints I can wrap them in an NSNumber at some unknown cost in
performance, but with NSRanges I'm sort of stuck. I hope to heck I
don't have to resort to stashing pairs of NSNumbers instead of
storing nice clean NSRanges. I don't even know how I'd do that
cleanly. Arrays of 2-item arrays?
Try:
NSArray *myArray = [[NSMutableArray alloc] initWithCapacity:10];
NSRange aRange = NSMakeRange(10,20);
[myArray addObject:&aRange];
Don't do this! The intention is good, but the logic is incorrect.
(It would not compile anyway.)
NSArrays must be given *object* pointers, not just any pointer.
NSRange is a struct, not an object. Structs are a C thing, so Carl,
that is a topic you need to study in C.
Even if you hack the above code to force it to compile, your program
would break almost immediately. There is a memory-management concept
in Cocoa called "retain and release." This is a whole study topic in
itself, but to put it briefly, NSArray "retains" elements that are
added and "releases" them when they are removed. Retain and release
only work with objects.
There isn't a built-in Cocoa class that will allow you to manage
arrays of NSRange structs. I was going to rattle off a variety of
approaches, but having just spotted Itrat's suggestion to use
NSStringFromRange() and NSRangeFromString(), I'd say that's the best
combination of simple and clean to get you going right now.
--Andy
_______________________________________________
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.