2

In Objective-C, I sometimes forget the @ that defines constant strings. See this example :

NSArray *colors = [NSArray arrayWithObjects:
                      @"Red", @"Green", "Blue", @"Yellow", nil];

In the example, I forgot the @ in front of the "Blue" string. This leads to annoying "Bad Access" bugs.

How to prevent these issues ? Is there some way with XCode to detect these strings or to display warnings ?

1
  • 1
    Proper testing prevents this from making it to release. Beyond that, you're on your own. Commented Jul 8, 2013 at 10:17

2 Answers 2

4

Run the analyzer, it'll show this:

Argument to 'NSArray' method 'arrayWithObjects:' should be an Objective-C pointer type, not 'char *'

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

Comments

3

It seems to me that the literal syntax considers that an error

NSArray *colors = @[@"Red", @"Green", "Blue", @"Yellow"];

Like seen below

enter image description here

1 Comment

It gives an error because it uses arrayWithObjects:count: which expects const id[] as the first parameter.

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.