0

I have java code which looks something like this:

for(int i=0;i<codeWord.length;i++) {
  codeWord[i] = cw_bArray;
}

here the codeword is NSDATA & cw_barray is NSMutableArray and in Objective c i am doing

for(int i=0;i<codeword.length;i++) {
  [codeword objectAtIndex:i] =[cw_bArray mutableCopy];
}  

but it is popping a error like this
Assigning to 'readonly' return result of an objective-c message not allowed

2 Answers 2

1

Praven S solution works fine if you are looking for adding object to your array. The java code seems more like a replacing object in the array by new ones. In order to achieve that you should use: [codeword replaceObjectAtIndex:i withObject:[cw_bArray mutableCopy]];

Like he said, objectAtIndex is a getter method. Hence you cannot assign a value to the method result. It would have no sense. Basically what you wrote is give me the i object of the array but you know what? Let say this object is another one.

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

2 Comments

how to write switch condition for this for(int i=0;i<codeWord.length;i++){ switch (codeWord[i]) but if the give same in objective c it is popping error Subscripted value is not an array, pointer, or vector where codeword is NSData
Instead of codeWord[i] you have to use [codeWord objectAtIndex:i] to access the element at index i. You also could use: for(ElementType element in codeWord) { switch (element) ... } The thing is, in Objective C you cannot use switch on other expression than resulting a number. Hence you cannot perform the switch directly on the element if it is not a number
1
[codeword insertObject:[cw_bArray mutableCopy] atIndex:i];

should do the trick for you.

Basically objectAtIndex returns a read only copy which cannot be assigned to another object. What you need to do is replace the entire object at that index with your new object.

2 Comments

how to write swtich condition for this i am trying to this switch (codeword.length[i] ) but in java they have mentioned switch(codeWord[i]) so confused
I dont know if i understood your comment and question correctly. but i guess this is what you want. [codeword objectAtIndex:i].count

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.