• 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: Accessing variables across classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing variables across classes


  • Subject: Re: Accessing variables across classes
  • From: Florent Pillet <email@hidden>
  • Date: Mon, 19 May 2008 15:30:45 +0200

Brett Powley wrote:
MyAppDelgate *ad = [NSApp delegate];
then do something with [ad myMutableArray]

Incorrect. You don't get accessors for instance variables automatically like that.

Well yes, but I assumed that his problem was that he didn't know how to get at the application delegate from his views, not that he didn't know how to write accessors. (How do you know that myMutableArray isn't an accessor method anyway?)

Key-Value Coding to the rescue. If you at least know the ivar name, you can do:


NSMutableArray *ma = [[NSApp delegate] valueForKey:@"myMutableArray"];

This looks quick and dirty, but in some cases KVC comes in handy. Remember to check the class of the returned value if you're not sure. Using KVC, you can read/modify any ivar of any object regardless of whether it has an accessor method defined or not. Not that I recommend this approach, though (for ethical reasons, mainly :-))

Florent

_______________________________________________

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


References: 
 >Accessing variables across classes (From: Joey None <email@hidden>)
 >Re: Accessing variables across classes (From: Brett Powley <email@hidden>)
 >Re: Accessing variables across classes (From: Andrew Farmer <email@hidden>)
 >Re: Accessing variables across classes (From: Brett Powley <email@hidden>)

  • Prev by Date: RE: Guidance for Cocoa's steep learning curve
  • Next by Date: Re: Cocoa et al as HCI usability problem
  • Previous by thread: Re: Accessing variables across classes
  • Next by thread: Re: Accessing variables across classes
  • Index(es):
    • Date
    • Thread