Skip to content

Posts tagged ‘Object-C’

27
Jun

Object-C Categories: Why and How

Did you ever wonder what an Object-C Category is good for and how to actually code and use one? Here is a short rundown of why I needed one and how I actually did it including using a property in my category which is per se not allowed within a Category.

Cheers – Andy

25
Jan

iPhone Crashes in the Nowhere Land

On Sunday I started to have a closer look at the memory management of the application and fully implemented the dealloc methods. Subsequent testing revealed a nasty bug where the application just died when I moved from one part of the application do another but I had not idea why it was crashing and especially where.

Well, at least I had a hunch where it was happening XCode did not point near there. Eventually I started to set breakpoints in all the dealloc methods that I could think of would be called. Eventually I saw that one property was already released and my release made it crash. So I just tried a few things and eventually found the resolution in calling the super class’ dealloc method at the end. So far I did not find any clear instruction if to call it first or last but it seems to be that way that it needs to be called last.

There are a few other things I have to keep in mind when dealing with memory management:

  • Use properties when dealing with references because it does handle re-assignments by itself
  • With properties every assignments to a property must be done with self like this: self.myProperty = …. This way we make sure that the setter method is called and subsequently the object is retained (if marked as such)
  • Called [super dealloc] as last statement in the object’s dealloc method (if provided)
  • Every object creation with alloc or new must be autoreleased when assigned to retaining property

Well, no I can go back and chase some more bugs.

-Andy

19
Jan

Object-C / iPhone and Switch Statements

Lately (meaning the last year or so) I started to be a lousy blogger. Either I have nothing to say or I am so busy that I don’t have time for it. So this time where I started to develop and iPhone app for hire with a hard deadline leaving a lot of work to be done. So far the project progresses well and there is little head banging. Still I ran into an old strange issue with Object-C and wasted a few hours. Eventually I dawned on my that I had this issue beforehand and then it was fixed right away.

So what happened? I wanted to create an enum to store a flag of what the program should do next like display the row, display the row’s detail or query the server for data. That looked like this:

enum Actions {
    row,
    detail,
    search
};
typedef enum Actions Actions;

Then I wanted to use it in a switch-case statement to execute the appropriate action:

switch( actions ) {
    case search:
        NSString *query = (NSString *) action.data;
        return;
    case detail:
        childViewController = [[TestViewController alloc]
              initWithNibName:@"TestDetail"
                                bundle:nil];
        break;
    default:
        NSDictionary *venue = (NSDictionary *) action.data;
        childViewController = [[Test2TableViewController alloc]
                                      initWithRows:rows];
}

But then I get this error: Expected expression before NSString which looks wrong. I am not sure why but the fix is simple. One just needs to wrap each block between case ???: and break into a {} block. That’s it:

switch( actions ) {
    case search:
        {
            NSString *query = (NSString *) action.data;
            return;
        }
    case detail:
        {
            childViewController = [[TestViewController alloc]
                  initWithNibName:@"TestDetail"
                                   bundle:nil];
        }
        break;
    default:
        {
            NSDictionary *venue = (NSDictionary *) action.data;
            childViewController = [[Test2TableViewController alloc]
                                      initWithRows:rows];
        }
}

Hope that helps - Andy