• 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 you @synthesize simple arrays?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can you @synthesize simple arrays?


  • Subject: Re: Can you @synthesize simple arrays?
  • From: "Michael Ash" <email@hidden>
  • Date: Mon, 17 Nov 2008 12:15:11 -0500

On Mon, Nov 17, 2008 at 10:20 AM, Clark Cox <email@hidden> wrote:
> On Mon, Nov 17, 2008 at 7:14 AM, Greg Robertson <email@hidden> wrote:
>> I have a couple of simple ivar arrays:
>>
>> NSString   *myString[4];
>> NSInteger   myInteger[4];
>>
>> How can I @synthesize these so I can access them with simple dot notation:
>
> I'm not sure that you can. However, this should get you the interface
> that you want:
>
> @interface MyClass :NSObject {
>  NSString *myString[4];
>  NSInteger *myInteger[4];
> }
>
> @property (readonly) NSString **myString;
> @property (readonly) NSInteger *myInteger;
> @end
>
> @implementation MyClass
>
> -(NSString**)myString {
>  return myString;
> }
>
> -(NSInteger*)myInteger {
>  return myInteger;
> }
>
> @end

This will "work", but because the properties only apply to the
accessing of the actual array, not the use of the individual elements,
no memory management will be done for the NSStrings and code which
just does straight assignments is likely to leak or crash, or both.
This may a good way to do it for primitives, but it's bad for objects.

I'd also say that this is a bad approach in general (not to criticize
your code, but just the original request) because you are essentially
bypassing encapsulation and giving callers direct access to your
internal data. There is no way to find out when someone has assigned
to the variable, so you can't override the behavior and provide
different ones if you need to in the future.

I'd recommend either using an NSArray even though you don't want to,
or writing accessors with indexes that you'd call like this:

[self setMyString:@"foo" atIndex:3];
[self myIntegerAtIndex:4];

Mike
_______________________________________________

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: Can you @synthesize simple arrays?
      • From: Ken Thomases <email@hidden>
References: 
 >Can you @synthesize simple arrays? (From: "Greg Robertson" <email@hidden>)
 >Re: Can you @synthesize simple arrays? (From: "Clark Cox" <email@hidden>)

  • Prev by Date: Re: Trouble with openFileWithoutUI
  • Next by Date: Re: NSButtonCell slowdown in NSTableView
  • Previous by thread: Re: Can you @synthesize simple arrays?
  • Next by thread: Re: Can you @synthesize simple arrays?
  • Index(es):
    • Date
    • Thread