Finding substrings in iOS

It one point I experimented with using titles on my answer buttons to keep track of which answer was selected. I switched to tags since they are integers and they are easier to use in this particular implementation. In any case, I though it interesting to show how to get a substring from a string. My titles are of the form, ‘Answer1’, ‘Answer2’, … ‘Answer16′. I really only need the last one or two digits. I pass an NSString into the delegate method from a button press.


- (void)checkBoxTapped:(UIButton *)buttonPressed {
    [[self delegate] quizAnswered:buttonPressed.currentTitle];
}

Then I grab either the last digit or the last two digits from the string.


NSString *answer = [selectedAnswer substringWithRange:NSMakeRange(6, 1)]; // wrongAnswer - wA
if (selectedAnswer.length == 8) wA = [selectedAnswer substringWithRange:NSMakeRange(6, 2)];

I was doing a string comparison to decide which question was answered,


if ([answer isEqualToString:@"1"] || 
    [answer isEqualToString:@"2"] || 
    [answer isEqualToString:@"3"] || 
    [answer isEqualToString:@"4"] ) {
    // do something
}

What I should have done is convert the string to an int and then do a simple comparison.


NSInteger answerAsInt = [answer intValue];
if ([answerAsInt < 5 ) {
    // do something
}

Null objects and NSArrays


- (void)displayWithParentView:(UIView *)parentView
                    withItems:(NSArray *)items
                 withOldItems:(NSArray *)oldItems  {
    
    NSString *pictR1C1Name = items[0];
    NSString *pictR1C2Name = items[1];
    NSString *pictR2C1Name = items[2];
    NSString *pictR2C2Name = items[3];
    
    UIButton *oldR1C1Button = (oldItems[0]  == (id)[NSNull null]) ? nil : oldItems[0];
    UIButton *oldR1C2Button = (oldItems[1]  == (id)[NSNull null]) ? nil : oldItems[1];
    UIButton *oldR2C1Button = (oldItems[2]  == (id)[NSNull null]) ? nil : oldItems[2];
    UIButton *oldR2C2Button = (oldItems[3]  == (id)[NSNull null]) ? nil : oldItems[3];

This is a snippet of the code I use to put pictures on the screen. The old pictures slide off the screen and the new ones slide on. When the screen is first initialized the oldItems array has not yet been initialized. So it is null. I can’t put a null object into a button though, so I test to see if there is an null object in the array and then set the button to nil. Otherwise, I set the button to the button that has been passed in from the array.

Likewise, I can’t put null objects into an array—recall that the way to define an array is


NSArray *newArray = [NSArray arrayWithObjects:object1, object2, nil];

The first nil object terminates the array. So to add nil objects to the array you need to first convert the nil to an object.

So you basically do the reverse of the code above to convert nil to an null object.


object1 = (object1  == nil) ? [NSNull null] : object1;
NSArray *newArray = [NSArray arrayWithObjects:object1, object2, nil];

Donating Platelets

I’ve been giving blood for a while now and it’s a pretty easy and painless thing to do. The blood bank used to be on the way to Taco Bell and the bank, so I’d stop by every eight weeks on my lunch hour. The giving blood part only takes about 15 minutes but the questionnaire and waiting after giving means that about an hour total is used up. I switched to doing an R2 donation a few years ago because it meant that I only had to go every 16 weeks and the amount of time actually donating is only about 45 minutes, so I saved a half hour of time. A few months ago they were desperately short of platelets and asked if I could give. I didn’t have anything else to do that day so I went home and got my laptop and donated.

It’s a little bit uncomfortable, but mostly because you have to stay in one place for 2 1/2 – 3 hours. When the blood leaves your body and goes to the machine, it feels just like when you are giving blood. In other words, no sensations at all. But when they put the red blood cells back in you get a weird sensation, almost like having a couple glasses of wine. Not unpleasant but weird. You can donate every two weeks, but that’s a bit too often for me, so I go once a month unless they have a critical shortage.

If you haven’t given blood before or haven’t done an R2, I’d recommend starting slow and working up. If you have given blood before, go for it. The platelets are used in patients undergoing chemotherapy or who have had organ transplants.

Here’s Flat Stanley and me donating.

Flat Stanley and me donating platelets

Things I can’t remember – Permissions

We recently migrated our sites to Linode. Since we switched distros from Gentoo to Ubuntu we had to add the users in by hand. That meant that the permissions on the old system didn’t match the permissions on the new system. In most cases, I want the person who owns the site to be the owner of the files and the group for the site be able to read and write the files. I usually put myself, artists, and system maintainers into the group. Everyone else, including Apache, gets read access. To do this manually is a real pain. Fortunately, there is an easy way.

To change the permissions on the directories go to the site you want to change and use:


sudo find . -type d -exec chmod 775 {} \;

To change the permissions on the files go to the site you want to change and use:


sudo find . -type f -exec chmod 664 {} \;

You need sudo since you probably aren’t the owner of the files. That’s it. The commands work recursively from wherever they are started.

Things I can’t remember – Backup MySQL

We used to do a cron job every night that made a copy of the MySQL database. Since we moved to Linode for hosting, we don’t need the cron job, but every once in a while I make a backup of the database and download it to my computer. This is the command I use.


mysqldump -u backup -A --default-character-set=utf8 -e --create-options >/srv/mysql-backup.sql