Removing items from NSMutableArray’s

I have an NSMutableArray in Objective-C that I want to remove items from so that it has just the number of items that will be displayed in a grid on the screen. The number of items varies, but on one screen it is 9. The array has more items, but I don’t necessarily know how many. [Remember that array items are numbered with an index starting at 0. So the last index number of my array of 9 items is 8′]

This is the original code that I tried.


NSUInteger itemsOnScreen = 9;
for (NSUInteger i = itemsOnScreen; i < gridItems.count; i++) {
    [gridItems removeObjectAtIndex:i];
}

This code doesn’t delete all of the items so I decided to start at the end and delete items until I there were only 9 left. This is the code that I ended up using.


NSUInteger itemsOnScreen = 9;

for (NSUInteger i = gridItems.count - 1; i > itemsOnScreen - 1; i--) {
    [gridItems removeObjectAtIndex:i];
}

It’s kind of ugly and after working with it it occurred to me why the original code didn’t work. As I started deleting items, the number of items, and hence the index of the last item changed. So it worked fine for the first few, but as I neared the end of the loop, there were no items with an index that matched i. If I just delete the first item after the 9 that I want, then the loop works fine.


NSUInteger itemsOnScreen = 9;
for (NSUInteger i = 0; i < gridItems.count; i++) {
    [gridItems removeObjectAtIndex:itemsOnScreen];
}

all mail servers must have a PTR record with a valid Reverse DNS entry

After we moved from our own server to Linode I started getting these bounce messages from Comcast. Reverse DNS just means that if you have an IP address you can find the domain associated with it. In my case I have lots of domains at the same IP address but use one for sending mail. I didn’t know what a PTR record is so I looked it up. Wikipedia says that “IPv4 uses the in-addr.arpa domain and the ip6.arpa domain is delegated for IPv6. The process of reverse resolving an IP address uses the pointer DNS record type (PTR record).”

On our old server we did our own DNS hosting and all of the info was contained in a file. So I knew about MX records, A records, etc. Linode uses a control panel to do the DNS setup so the setup is a bit different, but the same info is there. I had an A record for the domain that was sending the email, mail.machinename.com, and one for the domain that the email was coming from i.e. support@mycompany.com.

I checked with intoDNS to make sure that everything was set up correctly and I did in fact have a PTR record that pointed to the right place. “Your reverse (PTR) record: 192.168.255.173.in-addr.arpa -> mycompany.com”.

The clue came in a bounce from a different domain. The message said that the reverse DNS for 2500:34e80::f03c:92ee:fg70:ab93 was not found. It turns out that this is an IPv6 address. It appears that Linode is using an IPv6 address for the port I send mail from. To get a reverse DNS set up for that address I needed to set up an AAAA record in the Linode control panel, wait a while for it to propagate to Linode’s DNS servers, then set up reverse DNS for the IPv6 address.

I waited a day for the change to propagate and now my Comcast emails are going through fine.