• 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: objectAtIndex quandry
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: objectAtIndex quandry


  • Subject: Re: objectAtIndex quandry
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Sat, 22 May 2004 02:27:28 -0700

Hello...

Autoreleased objects (like the NSArray that resulted from sending the componentsSeperatedByString: message to theString) have a tendency to wander away while you are "waiting around" if you don't do something to keep them in line.

It would probably help if you change the line

theArray = [theString componentsSeparatedByString:@" "] ;

to

theArray = [[theString componentsSeparatedByString:@" "] retain];

When you are done with that particular array you would want to release it, before you use theArray to point to a different NSArray.

Technically, theString should probably be retained as well if you intend to use it again and eventually released, but it's not causing a problem at the moment since it's pointing at a constant string.

You might want to consider creating setter methods for your instance variables, to handle the retain/release stuff, if your updateTheStringDisplay: is going to be used multiple times.


Alastair's Cocoa FAQ has a quick summary of memory management and links to more comprehensive articles:
http://www.alastairs-place.net/cocoa/faq.txt
The relevant section is 3.1.1 under the heading "Memory Allocation"


Hope that helps,

Louis


Here is a clarification for the skeptics. I made a class (properly hooked up to the NIB, I assure you). The files ("AppController") are below. Started fresh with a new "Cocoa Application". Now I get the more meaningful but equally frustrating error:

2004-05-22 02:46:53.706 Test[670] *** -[NSCFNumber objectAtIndex:]: selector not recognized

My NSArray has turned into an NSCFNumber while I was waiting around. This looks like a pointer issue. What don't I know about cocoa?
...
- (IBAction)updateTheStringDisplay:(id)sender
{
theString = @"bob carol ted alice fred wilma barney betty bambam" ;
theArray = [theString componentsSeparatedByString:@" "] ;
[theStringDisplay setString:theString] ;
[theElementDisplay setStringValue:[theArray objectAtIndex:0]] ;
}

- (IBAction)updateTheElementDisplay:(id)sender
{
[theElementDisplay setStringValue:[theArray objectAtIndex:3]] ;
}
_______________________________________________
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.


References: 
 >objectAtIndex quandry (From: James Stroud <email@hidden>)

  • Prev by Date: Re: objectAtIndex quandry
  • Next by Date: Re: Menu-less popup buttons
  • Previous by thread: Re: objectAtIndex quandry
  • Next by thread: objectAtIndex quandry
  • Index(es):
    • Date
    • Thread