• 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: Instance an from an NSString Class name
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Instance an from an NSString Class name


  • Subject: Re: Instance an from an NSString Class name
  • From: Seth Willits <email@hidden>
  • Date: Tue, 21 Sep 2010 22:07:05 -0700

On Sep 21, 2010, at 9:21 PM, email@hidden wrote:

> 		NSString *class = [NSString stringWithCString:[data bytes] encoding:NSASCIIStringEncoding];
> 		Class obstacle = NSClassFromString(class);
> 		id thisObstacle = [[obstacle alloc] initWithImage:[sender draggedImage] andLocation:[sender draggingLocation]];
> 		[m_obstacles addObject:thisObstacle];
>
> I don't think I have to retain thisObstacle as m_obstacles take ownership.

No, but you'd better release it after you add it to the array otherwise you're leaking it. There's no such thing as "taking ownership" in an exclusive sense. m_obstacles simply retains it when you added it to the array. But the retain count is already 1 from the alloc, so now it's at +2. If you remove it from the array it's at +1 forever. You need to balance that alloc.



> However, must I retain [sender draggedImage] so the code becomes:
>
> 		NSString *class = [NSString stringWithCString:[data bytes] encoding:NSASCIIStringEncoding];
> 		Class obstacle = NSClassFromString(class);
> 		id thisObstacle = [[obstacle alloc] initWithImage:[[sender draggedImage] retain] andLocation:[sender draggingLocation]];
> 		[m_obstacles addObject:thisObstacle];


Also no. The obstacle would retain it, if needed, as Jerry said.



--
Seth Willits



_______________________________________________

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: Instance an from an NSString Class name
      • From: email@hidden
References: 
 >Instance an from an NSString Class name (From: email@hidden)
 >Re: Instance an from an NSString Class name (From: "Stephen J. Butler" <email@hidden>)
 >Re: Instance an from an NSString Class name (From: email@hidden)

  • Prev by Date: Re: Instance an from an NSString Class name
  • Next by Date: i5/i7 w/External 24" LED Cinema identifying screens - testing help
  • Previous by thread: Re: Instance an from an NSString Class name
  • Next by thread: Re: Instance an from an NSString Class name
  • Index(es):
    • Date
    • Thread