• 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: NSArray as a static
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray as a static


  • Subject: Re: NSArray as a static
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Mon, 1 Sep 2008 20:38:38 +0200

Unlike java, Obj-C does not have the concept of class variable.
Your static variable is a classic C variable, and C variable are not automatically initialized to NULL.
The first time you call init, relationshipMatch may contains anything and may not be NULL, and so it will never be properly initialized.


Second error, you do not retain you variable. You create it using a convenient initializer wich returns an autoreleased array. At the end of the event loop, your array is released, and as you never retained it, it is deallocated.



Le 1 sept. 08 à 20:29, Richard Good a écrit :

I'm just learning Objective C and need to understand how to declare and use a static NSArray.
I think I may be confusing the Java concept of static and Objective C's concept.



What I want is how to use the Java idea of a class static variable in Objective C


Here's the code I'm trying to use:

//class name is Person
static		NSMutableArray*	relationshipMatch;

-(id)init {
	[super init];
	if (relationshipMatch==nil)
		[Person initRelationshipMatch];
	return self;
}
+ (void) initRelationshipMatch	{
	relationshipMatch = [NSArray arrayWithObjects:
		@"abc",@"def",@"hig",nil] ;
}


When I try to access the array using NSString* aString = [relationshipMatch objectAtIndex:1];

I get
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***


Any help would be appreciated.

Richard Good

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSArray as a static
      • From: Steven Noyes <email@hidden>
    • Re: NSArray as a static
      • From: "Adam R. Maxwell" <email@hidden>
    • Re: NSArray as a static
      • From: "Clark Cox" <email@hidden>
References: 
 >NSArray as a static (From: Richard Good <email@hidden>)

  • Prev by Date: Re: Newbie question: What does "no-op" mean?
  • Next by Date: Re: Problem with Info.plist in 10.5 [RESOLVED]
  • Previous by thread: NSArray as a static
  • Next by thread: Re: NSArray as a static
  • Index(es):
    • Date
    • Thread