January 2011

How to concatenate a string in Objective-C

I’m writing this article with the objective of hopefully helping you if you’re looking how to concatenate a string but also to vent just a tiny little bit.

Firstly, to set the scene. You have two strings and you want to join them together. For example, string1 is something like “Fly” and string2 is something else like “Digital” and you want to join them as string3 to become something new such as “Fly Digital”.

In ASP Classic, this is a breeze:

<%
string1="Fly"
string2="Digital"
string3=string1 & " " & string2
Response.Write(string3)
%>

In PHP, not a problem:

<?php
$string1="Fly";
$string2="Digital";
$string3 = $string1 . " " . $string2;
echo $string3;
?>

But Objective-C doesn’t quite use the same logic of string1 + string2 = string3. Instead you need to call a whole new method called stringByAppendingString, which is where I start to ask questions. But first, here’s how I do it in Objective-C:

NSString *string1 = @"Fly";
NSString *string2 = @"Digital";
NSString *string3;
string3 = [string1 stringByAppendingString:string2];

Now call me old fashioned, but I think the PHP / ASP Classic / Javascript methodology is far more easily adoptable for programmers than having to know a new method to achieve what I consider a very basic but fundamental part of programming. I question why the developers of Objective-C thought that a whole new method had to be invented rather than sticking to the very logical 1 + 2 = 3 style. If there’s anyone reading this that knows the answer to that question I’d love to hear it.

How to use your iPhone as a modem – known as Internet tethering

Travel a lot with a laptop and an iPhone and don’t want to use expensive wifi services to check your emails? Next time why not try “tethering” your iPhone to your laptop and use it as your 3G modem.

It’s really simple, do the following on your iPhone:

  1. Go to “Settings”
  2. Click “General”
  3. Click “Network”
  4. Click “Internet tethering”
  5. Switch to ON

You can then connect the iPhone to your laptop via Bluetooth or the USB cable. I’ve personally struggled a little with the Bluetooth option but USB has always been great.

Once you’re connected, you should see the screen shown here, and the status strip at the top of the screen should be flashing blue and saying “Internet Tethering”.

Internet tethering on an iPhone 4

If you have problems, I’ve found in the past that connecting and activating tethering then restarting the PC/Laptop when the iPhone is still connected helps the machine recognise the device.

Sent from my laptop tethered to my iPhone.

How to loop through files in a folder in PHP (the quick way)

If you’ve ever used FSO (File System Object) in ASP Classic to loop through files in a folder you’ll know how useful it is. You can create a script with parameters to check file types, names, sizes, etc and then all your client needs to do is manage the source directory on the web server to update the site.

PHP is no different in principle, but it took me a little while to get the syntax down nice and tight to a couple of lines to do the same thing, so I thought I’d share how we do it here.

This is the base script that I use to setup the directory and the loop:

<?php
 $dir = "../yourDirectoryName/*";
 foreach(glob($dir) as $file)  
 {  
 echo $file;  
 }
 ?>

Note the $dir variable is set as the directory where your files are located on your web server in relation to the script.

Once you have the basic loop, you can then easily adapt it slightly to do different things. Eg, say you’re looping through a list of images and you want them to show on the page, change the echo statement to this:

echo "<img src='$file' alt='$file'/>";

Or say you only want to show images in a directory whose filename contains “_thumb”, add an IF condition around your echo statement like this:

if (strpos($file,"_thumb") >0) {
  echo "<img src='$file' alt='$file'/>";
}

The possibilities really are endless. Give it a try and let us know how you go.

TYD3RNPHX7SB <!–b1cfc4ed59be410f9d457cfd3a385507–>

Shipments of smart phones to exceed PC’s in 2012

PhoCusWright are soon to release a report stating that shipments of smart phones are expected to exceed PC’s for the first time in 2012.

Additionally, the report will state that 3 out of 4 active travelers use a mobile device while traveling and 2/3 say they are likely to research, shop and book travel activities via their mobile devices.

It’s really feeling like 2011 is the year of getting your mobile strategy in place to exploit this groundswell of mobile activity that’s being predicted.

See the full article on the tourismexchange.com website here:
http://www.tourismexchange.com/exchange/en/newsroom/home/getArticle.jsp?articleID=13502&languageID=1

Google Universal search layout change

Noticed a change of layout on google.com.au for how they are displaying universal (mapping) search results.

Location based searches like this example for “hotel sydney” now show the map result at the top of the right column:

Google universal search result layout 1

Interestingly when you scroll down the page the map remains in the same position in the browser effectively covering positions 4-10 of the paid (SEM) search results:

Google universal search result layout 2

This is an improvement over the previous layout for those ranking well organically as more organic results can be seen above the fold when a map appears. Conversely however, I’m not sure how happy advertisers whose ad creative is now covered when you scroll down the page will be. For example, in the second screenshot note the paid ad for Rydges Hotels – www.rydges.com – is now partially covered.

How to include one CSS file in another CSS file

So you’re familiar with the include function in your favourite scripting language, but did you know you can use the same principle with your CSS files?

A CSS include works in exactly the way you would expect. Add this line of code to your CSS file, change {filename} to be the name of the CSS file you want to include (without the curly brackets) and bingo!

@import "{filename}.css";

I use this when I’m working on large sites that have some common CSS styles across the site but some that are specific to sections. I usually have one master.css file and multiple sectionname.css files. This makes editing styles that are not generic to the whole site a much quicker process.

How to center a website in CSS

A lot of websites that we build require the site to be centered regardless of the total screen width. I remember in the days of tables we’d simple put a master table of 100% width on the page, add a TD with an align=’center’ tag then nest another table inside to centre the main content.

With CSS the principle doesn’t have to be too different but the code is a lot neater. Here’s how I usually do it:

1) Create two CSS divs, one for the master container and one for the content:

div#container
{
 margin: 0 auto;
 background-color:#fff;
 width:600px;
}
div#content
{
 padding:20px;
}

The margin:0 auto; is the important part here. That is what will center the content within. The background-color and width attributes of the container div and padding attribute of the content div are just to highlight the example, you can remove these or change them to suit your design.

2) In your HTML, add your divs into the page BODY:

<body>
 <div id="container">
  <div id="content">
   Hello World!
  </div>
 </div>  
</body>

And that’s it!

Here’s the full code:

<html>
<head>
<style>
body
{
 background-color:#000;
}
div#container
{
 margin: 0 auto;
 background-color:#fff;
 width:600px;
}
div#content
{
 padding:20px;
}
</style>
</head>
<body>
 <div id="container">
  <div id="content">
   Hello World!
  </div>
 </div>  
</body>
</html>

Hello (Blog) World

Traditional Hello World first post to test the template. I’ll post something slightly more meaningful shortly…