• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't cram an NSRange into an NSArray?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Re: Protecting Software w/ Software License Keys...
  • Next by Date: Re: Can't cram an NSRange into an NSArray?
  • Previous by thread: Re: Can't cram an NSRange into an NSArray?
  • Next by thread: Re: Can't cram an NSRange into an NSArray?
  • Index(es):
    • Date
    • Thread