Category: Issues

Xcode Code Snippets

Xcode appreciation is not universal. @wtfxcode, for example, is a Twitter account relating various issues (and/or frustrations). Also, I often see threads of discussions between developers on how JetBrain’s AppCode is so much superior.

For my part, I never felt the need to switch to a new IDE. Albeit not perfect (no software/IDE is), Xcode fits my needs well.

Having said that, this post is about a bug I encountered in Xcode.

Xcode has a built-in code snippets library in the Utilities bar. (And you can add your own snippets too. See this very useful answer on stackoverflow.com). I use it mainly for Core Data fetch requests. Drag and drop, and press tab to replace parameters for your own variables.

One time I got compiling errors over this (completed) code snippet:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>" inManagedObjectContext:<#context#>];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *fetchedObjects = [<#context#> executeFetchRequest:fetchRequest error:&error];             // compiling error at this line
if (fetchedObjects == nil) {
<#Error handling code#>                  // compiling error at this line
}
[fetchRequest release];

I stared at my code for a while, making sure that the few code I did input in there was correct, and indeed it was. I couldn’t get where the error could have come from. I tried to manually re-type the same code. The errors were gone.

Advertisements

checking NSError for nil

We’ve all seen this code:

NSError *error;
// do something...
if (error) { // act on error if any, but crashing instead... }

There should be nothing wrong with this code snippet. Except that there can be. I’ve countered many occasions where error was not initialized to nil by default, the if statement was evaluated to true and associated code was executed. But since there was no error, I got runtime errors, many of which were EXC_BAD_ACCESS…

Changing to:

NSError *error = nil;

made my code run fine.

In the literature I’ve read, all variables in Objective-C are initialized to nil, even if not done explicitly. This seems to be not true anymore. Does anyone have an answer to that? Please share in the comments if you do. My theory is because of the new LLVM compiler, but please prove me wrong if I am.

Let’s get technical!

I haven’t talked about what my app will be yet. And I have been very shy on detailing my progress. Even though it’s the main purpose of this blog, it often felt worthless… I don’t write as much as I would want to, so when I do, I talk about things that seem more useful.

Today I came accross an issue. Actually I have been having this issue for a few days, but until today, I could avoid it. So I did.

I tried to debug as much as I could, checking the debug navigator, outputting everything that seemed relevant to the console, trying to analyse the call stack so I could figure out what the app was trying to accomplish… Because of course, the error I got was EXC_BAD_ACCESS code = 1. Which means: you have a memory error, now find it.

Where do I go to? Being an indie developer, I can’t turn to a collegue. I don’t do code review. An old collegue of mine use to help me out via screen sharing, but he’s charging me… I finally turned to my best friend stackoverflow.com. I asked my question, trying to be as detailed as possible. But I am always shy to ask a question there! I am always afraid I will look stupid, too new to do anything useful, or just helpless! I am always glad someone asked the same question I have before me so I can just read the answer and be happy!

I create custom views on one of my UIViewController. These views have two labels that I set with specific values; one int and one string. In the first iteration of the UIView class, the int (called number) was an actual int. So the property was declared:

@property (assign, nonatomic) int number;

Later on, I changed number to be an NSNumber, but didn’t change the property declaration. It caused a memory error when the view was accessing _number to set the label properly. Changing the property to:

@property (strong, nonatomic) NSNumber *number;

fixed my issue.

Back to coding!