1

I have this method:

+ (NSData *) createWave: (short[])sampleData {
    int i = [sampleData count]; // Warning: Invalid receiver type 'short int *'
}

Inside this method, I'm trying to determine how many elements are in the samples array that was passed in. But I'm getting the warning above (I get the same warning if I change samples to short *).

How can I pass an array like this, and then determine the array's size?

2 Answers 2

5

You can't.

Either make sure that the last element in your array is unique and check for that or pass in a size parameter as well i.e.

+ (NSData *) createWave:(short [])samples size:(size_t)count {
    int i = count;
}

short[] isn't an object so you can't call methods on it - that's why you're getting a warning (and probably a crash if you run the code!)

Sign up to request clarification or add additional context in comments.

3 Comments

You should provide the type to count, otherwise it defaults to id.
+ (NSData *) createWave:(short [])samples size:(size_t)count;
Oops, that'll teach me to type quickly! I've typed it as size_t :)
3

You are trying to use a C style array as a parameter and then access it as an Objective-C object. (I am assuming sampleData and samples are supposed to be the same). Use an NSArray of NSNumbers instead because with C style arrays you need to know the length.

+ (NSData *) createWave: (NSArray*)sampleData {
    int i = [sampleData count];
}

2 Comments

Yeah, that was a typo, thanks. I'm avoiding using NSArray here for performance reasons - this is raw audio data, which can be quite large.
I understand and I gave the other answer +1 for that reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.