On Mar 8, 2015, at 20:55 , Patrick J. Collins <[email protected]> 
wrote:
>
> The compiler tells me the type doesn't match, and it seems I have to do this 
> instead:
>
>  -(double *)ks {
>    return (double *)_ks;
>  }
>
> Is there a way to get a property to get autogenerated but use that array?

Ah, I was afraid the compiler might complain. The error message usually says 
what each type is. If you can copy that to the thread, we might be able to come 
up with something (like throwing in a const somewhere, something like that) 
that satisfies it. If you don’t like defining the trivial setter yourself, you 
can try something like this:

> @implementation Reflector
> {
>       double myKs[11];
> }
>
> -(instancetype)initWithKs:(double *)ks rms:(NSUInteger)rms {
> …
>       _rms = rms;
>       _ks = myKs;
>       for (NSUInteger i = 0; i < 11; i++)
>               _ks [i] = ks [i];

About this:

> And lastly, if I do:
>
>  -(instancetype)initWithKs:(double *)ks rms:(NSUInteger)rms {
>      if (self = [super init]) {
>          _rms = rms;
>          memcpy(_ks, ks, sizeof(double) * 11);
>      }
>      return self;
>  }
>
> do I need to free the memory in dealloc?

No, because you never allocated anything, but just rode along on the object’s 
own allocation.

Incidentally, though the ‘memcpy’ isn’t a silly thing to do, it’s somewhat of a 
premature optimization. It’s quite possible that the compiler can optimize the 
‘for’ loop that I wrote to run faster than the memcpy. The for loop can copy 8 
aligned bytes per iteration, the memcpy might copy a byte at a time. The memcpy 
might also have a function-call overhead, though I suspect that it actually 
gets compiled inline.

C is fun. :)



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to