3

I'm trying to send information to a server using ASIHTTPRequest and am setting the post values like this:

    for(int i = 0;i<13;i++){
  [request setPostValue:propertyValues[i] forKey:propertyKeys[i]]    
}

propertyValues and propertyKeys are both NSArray objects that hold 13 items each. When I run this, I get the error "Subscript requires size of interface 'NSArray', which is not constant in non-stable ABI"

What does this mean?

Chris
  • 11,819
  • 19
  • 91
  • 145

4 Answers4

20

It means you're trying to access an NSArray* as if it were an array, or a pointer you could do arithmetic with. x[5] for pointer types is equivalent to x + sizeof(x) * 5. Note the sizeof operator - it means the compiler needs to know the size of the type to advance the pointer by 5 "items". However, NSArray is an Objective C interface, and the compiler can't be sure what the size of the type is. This explains the wording of the error you're getting.

More prosaically, you can't access NSArray objects like that. You need to send them a message asking for the item at a given index, thus:

[propertyValues objectAtIndex:i];
Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • Nothing like reviewing basic concepts in the morning! Thanks for the great answer! – Chris Jun 03 '11 at 15:32
  • 1
    Note, that this is no longer true. As of Xcode 4.5 it's possible to access NSArray elements using [index] directly. See this answer for more details: http://stackoverflow.com/questions/9347722/what-are-the-details-of-objective-c-literals-mentioned-in-the-xcode-4-4-releas – Nick Lockwood Nov 16 '12 at 20:14
3

You cannot access the objects inside an NSArray with the square bracket syntax. You have to use

[propertyValues objectAtIndex:i]

instead.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
2

To access elements of NSArray you must use objectAtIndex: method

for(int i = 0;i<13;i++){
  [request setPostValue:[propertyValues objectAtIndex:i] forKey:[propertyKeys objectAtIndex:i]l    
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
1

That's not now you access values in an NSArray (which is fundamentally different than a C array).

[request setPostValue:[propertyValues objectAtInstance:i] forKey:[propertyKeys objectAtInstance:i]];
kubi
  • 48,104
  • 19
  • 94
  • 118