0

I am new to objective C. I am writing code for a coding competition. For that, I am writing code in my XCode IDE and I upload the code on their website. The code that works in my machine gives compiler errors on the website.

For e.g. A simple assignment of array element which works for me in XCode.

NSArray *itemArray;
NSString *feet;
feet = itemArray[0];

The above line generates error "incompatible types when assigning to type 'struct NSString *' from type 'struct NSArray'"

I have to use the following syntax to make it work.

feet = [itemArray objectAtIndex:0];

Similary, the for loop.

for (int i = 0; i <= count - 1; i++) {
<#statements#>
}

This gives error "for loop initial declarations are only allowed in C99 or C11 mode". I need to make the following change to make it work.

int i;
for (i = 0; i <= count - 1; i++) {
<#statements#>
}

Could anyone please advise how do I configure my XCode IDE so that I can write the code compatible with the compiler in the website?

Apparently, the supported version of Objective C on the website is GCC 4.4.5 I would like to know whether I can configure it in XCode

7
  • please check: stackoverflow.com/questions/24881/… Commented Apr 26, 2016 at 7:38
  • Does your Objective-C file end in .m? Commented Apr 26, 2016 at 7:41
  • File extension is .m Commented Apr 26, 2016 at 9:15
  • @NitinGohel I tried to change "C Language Dialect" in the build settings. There are options like ANSI C, C89, GNU89, C99 and GNU 99. ANSI C and C89 gives a lot of compiler errors including errors in header files. Other options work fine. Commented Apr 26, 2016 at 9:41
  • Does it work with GNU99 ? Commented Apr 26, 2016 at 13:46

1 Answer 1

0

On your first exemple, you are just declaring your variables, but you need to create an instance for you array and fill it with some elements.

NSArray *itemArray = @[@"item1-String", @"item2-String"];
NSString *feet = itemArray[0];

Otherwise trying to get the first element of the array (with itemArray[0] or [itemArray objectAtIndex:0] will result in an issue because the array is not instanciated.

Note that if your array is meant tot by dynamic (add/remove elements later), use a NSMutableArray type

 NSMutableArray *itemArray = [@[@"item1-String", @"item2-String"] mutablecopy];

And if you don't know how many items contains your array, check its count before trying to get element :

NSString *feet;
if (itemArray.count <= 3) { //Is there at least 3 elements? 
    feet = itemArray[2]; //then get third element
}
Sign up to request clarification or add additional context in comments.

3 Comments

I left out a lot of code from my post as I just wanted to highlight the problem. I just want to know why itemArray[0] is giving compiler error.
What is then the type of the first item in your array ?
My array contains strings. It is initialized as NSArray *itemArray = @[@"A", @"B", @"C", @"D"];

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.