Ph: 46164136
Abe's

Code and Stuff

RSS Feed

NO MORE LOGS!!!!! (Using breakpoints in xCode)

1 Comment

I cant count the number of times I have compiled an app and found my self lost in logs. This is not not just an xCode issue I have come across this in every language/IDE I have used. For some reason we like to log it and leave it.

We give our selves all maner of excuses for not removing log statements. Whether its needed so every developer on the project knows whats happening or simply not  having time to go back and remove them. We do end up accumulating a lot of logs over the span of a project.

Dont get me wrong a well placed log can make debugging a problem much easier. There are other ways to do it though. Using breakpoints in xCode rocks logging as well as giving you a few extra perks.

Lets start with right click and editing a breakpoint to see:

Now this is one of my favorite tools in xCode. Off that bat it allows for conditional breaks. The above break for example will not break unless the variable newPos is equal to 42. This can help with keeping an eye on a value to make sure it does not act in appropriately and if does you will get the break. How ever clicking the actions text above marked with a blue arrow will unleash the true power behind the xCode breakpoint.

Out of the 5 above options I only use two with any frequency.  The “Debugger Command” which will allow you to log, inject calls and set variables.  The “Log Message” more for english messages and keeping track of the frequency of code used.  OH ya one more thing ”Log Message” supports text to speech.

Lets start with the “Debugger Command”:

“po someObject” looks like it would print the description of someObject. Thats not all though. Dont forget the conditional above it “newPos == 42″. Combine the two and you will only print someObject to the consol when newPos is equal to 42. If you uncheck the bottom checkbox “Option” it will log the description with out breaking.

You can also use the field to more than log. Be careful  though as this can easily cause problems if you forget to remove or disable the break.

The command could be to call a function or set a variable like this: “newPos = 13″

If you were to forget to remove a command like that it could burn a lot of time tracking it down.

Moving onto the “Log Message”: 

This obviously prints a message. If you look under text field to the right you can see a key. Above I used the %H which will be replaced by the number of times the break has been hit. Also if you remember to uncheck the Option at the botton it wont break but will simply log at the break.

You also get the option to have the message read out loud. This is useful if you have to keep your attention on the app and cant keep an eye on the logs.

This covers the two basic implementations. What if you need to do more than one thing like run multiple commands?

Stacking Actions:

Pressing the + button as indicated above by the orange arrow. That would be the top arrow as the bottom one is yellow. (my color selection is rubbish) You get another action that runs at the same time.  You can run any number of commands in the one break which can provide very rich loggin and debugging capabilities.

Managing your breaks:

Toggling the button indicated by the green arrow you can enable or disable all your breaks in one go.

The only way to manage large numbers of logs is to search nslog and edit the file. This will cause the file to have been touched wich will obviously need versioning. Fortunately if you open the break window indicated by the blue arrow above. A list of all your breaks and their locations can be found.

Each break can be enabled or disabled by pressing the break symbol to its right as indicated by the purple arrow above.

Sharing Breaks:

Most projects are version controlled with either SVN or GIT. In all of my projects we make sure to ignore the personal settings folder in the .xcodeproj file. Which incidentally is just a folder. You can right click “Show its Contents”. In there you will find a folder called “xcuserdata” this will contain the personal profile for each developer. It will contain  the developer specific xCode settings like layout. This folder is ignored and never committed or pushed.

However if you were to share a breakpoint.

Right click a break point and select share as seen above. It creates a new folder in the project file. The “xcshareddata” can then be used to share break points with other developers . Say you had a particularly nasty bug and set up some particularly elaborate breaks they would be easily passed on to another developer who could help you or continue on the work.

The description method

0 Comments

Any object can implement the description method that is used to return a description of the contents of a receiver. The problem i have always found with this method. Is keeping the method up to date during the development process. Also getting other developers working on the project also keeping it up to date.

With that problem in mind I put together this method that will automatically generate the string and list all available properties in the the receiver. It will print out when the instance is used in NSLog or or printed using the GDB debugger commands in my previous post.

//this will automatically create a description
//based on the properties
- (NSString*) description
{
    unsigned int propCount, i;
    //gets a list of all properties in the
    //created in this class and the property count
    objc_property_t *properties = class_copyPropertyList([self class], &propCount);

    //used to hold all the properties
    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:propCount];

    for(i = 0; i < propCount; i++)
        {
        //extracts a single property
        objc_property_t property = properties[i];
        //gets the property name
        const char *propName = property_getName(property);

        //checks to make sure there is a name
        if(propName)
                {
            //converts the name to a usable object
            NSString *propertyName = [NSString stringWithCString:propName
                                                        encoding:NSNonLossyASCIIStringEncoding];
            //adds it to a list of keys
            [keys addObject:propertyName];
        }
    }
    //frees the properties
    free(properties);

    //attempt to simulate the dictionary log by using the %p to get the memory address
    //the last %@ is used for all the items added to the dictionary
    return [NSString stringWithFormat:@"<%@: %p; %@>",
                                      NSStringFromClass([self class]),
                                      self,
                                      [self dictionaryWithValuesForKeys:keys]];
}

You can also find it on my custom xCode 4 templates here on github. Right at the bottom the class.

xCode GDB Debugger Commands

2 Comments

One of the things that I struggled with going from eclipse and Visual Studio to xCode was the the debugger.  Most of the objects have memory references rather than values. For a long time while developing iOS apps all i would get, or though I had was one cryptic error message. Till I discovered po which stands for print object. This allowed me to print out the contents of an object which can be very useful.

Here is  list of gdb commands: (Warning properties are not identified by the debugger so dont use anything with dot notation)

po – print object – can be used to print out the likes of NSStrings and NSDictionaries
use: po someObject

print – prints base types – used to print out to the screen ints floats and the like
use: print myFloat

set – set a variable - allows you to set variables
use:  set someString=@”test”

call – calls a method – can be used to call methods or the properties getters and setters
use: – call [someObjectInstance withString:@"newString"];

n – Next – always you to travers your code during breck
use: n

There are 2 main places you can use the commands.

The first is the consol output during a break or crash just after the gdb:

The second is in a break point first set a break point then click the “Click to add action text” ass seen bellow.

Now you can add you command in the newly available text field that will always be triggered at that point in the code.

Carful with the break point triggers not to confuse your self if you put sets or calls in there.

Setting up xCode 4 Projects

0 Comments

Every time I start a new xCode project i do it a little differently. I include files in some and settings in others. I have decided to put them all here to make sure i don’t miss any. If you have any other project settings, scripts, snippets or useful utils please drop a comment.

1. Enabling and dealing with Zombies:

This allows you to find errors that come from delloced objects being called upon. That normally just gives you the useless error bad access. with this enabled it should take to a class or function when you see the bad access error. This can be dealt with in 2 ways either enabling zombies in the editor thus allowing you to debug it in xCode or using instruments. If the first method does not work for you try the second.

Method 1 xCode Debug:

get to the run settings by press: cmd+alt+r from the left nave select “RUN  yourApp.app” select “Arguments” from the right tab system expand if not already expanded the environment variables add NSZombieEnabled the name parameter and YES to the value parameter

Method 2 instruments Debug:

Rather than take you through it Mark Johnson has created a good video that talks you through it.

This page contained an embedded video. Click here to view it.

Check out his blog post for more details: http://www.markj.net/iphone-memory-debug-nszombie/

2. Replacing NSLog with PSLog

PSLog is a NSLog replacment. I have seen it arround for a while now. Basicly it gives you a bit more info like where log is called from. The only problem with it, is the dependancy it creates on having to keep importing PSLog in every one of your projects.

I modified it so you can use NSLog in your code and at compile time in debug mode it replaces your NSLogs with PSLogs. During deploy it replaces NSLog with comments. This way it removes any depandacies on the PSLog function.

How ever this only applies to the PSLog function it self none of its expanded counter parts like PSLogDebug. Using PSLogDebug will maintain a dependancy.

Check it out:

https://github.com/abeazam/PSLog.git

If you #import “PSLog.h” in the .pch file in the support group in your project you wont have import it in every file you want to use it. which Helps the whole dependancy issue.

TODO, FIXME converted to warnings

I duno if you noticed but if you add a //TODO: or //FIXME: comments it appears  in the function menu. The problem with that though is you need to be in the class to see the list. Ideally what I wanted was a list of todos that I can see them all in one place. You can do that by adding a bash script that adds them as warnings.

Select your project in the left hand menu Select your project in the target right hand menu Click “Add Build Phase” in the bottom right. Select “Add Run Script” Past in the script below and your done :)
KEYWORDS="TODO:|FIXME:|\?\?\?:|\!\!\!:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($KEYWORDS).*\$" | perl -p -e "s/($KEYWORDS)/ warning: \$1/"

i think this guy might have come up with it: http://deallocatedobjects.com/2011/05/11/show-todos-and-fixmes-as-warnings-in-xcode-4/

Snippets
Snippets are one of the things that stood out to me in xCode. Here are a few of mine:

Completion shortcut – log
Completion scope – function and method

NSLog(@"<#value#>: %<#@#>",<#var#>);

Completion shortcut – imp
Completion scope – processor directive

 #import "<#header#>"

If you want to make a variable just stick them in <#here#>.

Crash Log Server

http://quincykit.net/

https://github.com/therealkerni/QuincyKit

Sends the crash report to a server with a single line of code. I have not tried the push notifacsion but every thing else seems to work resonably.

http://quincykit.net/

Please let me know if you find anything else that helps keep a project in shape.

Unix and SSH Files From Scratch

0 Comments

Again I am assuming some Technical/Programming experience. No Unix experience except for my previous post. I am going to cover some default files. As well as how to make you own bash file. I am going to try and explain this through a tutorial on how to add a welcome message to your login.

The first thing you need to do is decide on a message. You could just have a one line message like “Ola welcome to the server” which would be fine.

On the other hand if you really wana go at it. You could get some ascii art or text. Personally I am far from an ascii artist but I have ascii every where. There are a few websites that have ascii fonts. All you have to do is type in what you want and it will write it in ascii for you. http://www.network-science.de/ascii/ was the last site I tried. There are loads out there.

If you have administrative rights on your box creating your welcome msg is easy. If you dont have admin rights thats fine, you can use Option 2  which is also easy.

If you are unsure if you have admin access try and open an editor with admin access if it lets you in, BINGO your admin. Type in sudo pico enter your password when prompted if it lets into the pico editor then you have admin.

sudo pico



Option 1(if you are admin)

There is a file named motd. It is just a text file without the txt file extension. Anything saved in the file will be displayed when you log in.

Step 1 Navigate to your root of your system

 cd / 


Step 2 Navigate to the etc folder

 cd etc 


Step 3 Edit the motd file with admin access

 sudo pico motd 


Step 4 Type or copy/past your welcome msg in to your editor

Step 5 Save and exit

Now if you log off the server and log in again you should see your msg.

Option 2(if you are on a shared host or if you dont have admin)

So you dont have admin access and you want to trigger something at login. Well what you do is save the msg in a text file then prompt it to be displayed. There is  file that runs every time you log in named .bash_profile. What we will do is create a bash file that when run will display the text from the file. Now this is a bit of a round about way of doing it but it helps cover both bash files and the .bash_profile.

Anything you want to run at login can be added to the .bash_profile file.

A bash file is a Unix file that runs a group of commands. It has a language behind it that contains most of what you would expect if statements, loops, etc. Bash stands for Bourne Again Shell. you can use it to make your server practically do anything.

Step 1 Navigate to your home directory. This is the directory u start in when u log in.

Step 2 Create a new text file called login.txt

touch login.txt


Step 3 Assuming you have your msg ready or your ascii if not go get it se the links above.

Step 4 Open the the login.txt file in an editor

pico login.txt


Step 5 Past your msg save and exit pico

Step 6 Find the location of you bash

echo $SHELL


Step 7 Copy the return value it is more than likely this:   /bin/bash   but not necessarily

Step 8 Create a new file in you home directory again which is the bash script

touch login.sh


Step 9 Edit the bash script in pico

pico login.sh


Step 10 Type in #! then the bash location that you copied in Step 7

#!/bin/bash


Step 11 Type the command that will display the text

cat login.txt


Step 12 Save and Exit

Step 13 While still in the root set the permisions for bash file

chmod 700 ./login.sh


Step 14 In your home directory edit your .bash_profile so it runs the login bash at login time

pico .bash_profile


Step 15 Add to the bottom of the file the command that runs your bash

./login.sh


Step 16 Save and Exit

Thats all there is. Next time you login you should see your msg. This covers of the basics if you want to know how to write complex scripts there are loads of guides and tutorials to help you on your way out on the interwebs,

Filed under Server, ServerSide, ssh, unix

Unix and SSH Commands From Scratch

0 Comments

In my last post I discussed node.js. This can be installed on a server. Originally I wanted to talk through how I installed Node.js and what plugins I have come to use. I took a look around and found a few ppl struggling with ssh. I figured best to cover some of the basics of server based commands first. I expect any one reading this have some Technical/Programming experience.

I am going to assume you have a sever and are working from a Unix based machine(Linux or OSX).

Accessing your server:

ssh usr@host

usr: would be your user name to the server for the sake if this example lets say your user name is peter

host: is your server url/ip lets say for this example its Watson.com

so in your command window type in your server details press enter:

#these are fake details use your user name and server in their place
ssh peter@watson.com

Enter you password and you should be in.

Basic Commands:

Here is a list of basic commands to help you move around the folders and manipulate the files.

#this is how you comment if you where to write a script file
ls

Type that and it will list all the files and folders

ls -a

That will show all the hidden files too.

cd somefolder/

Primary navigation command.

cd ..

goes up a folder.

cd folderName

navigates to the folder with the name folderName

cd ~

Takes you to your home directory.

mkdir newDirName

creates a directory

cp txtFile.txt somefolder/copyOfFile.txt

copies file.txt to someFolder and renames the file.

mv txtFile.txt somefolder/copyOfFile.txt

moves file.txt to someFolder and renames the file.

rm txtFile.txt

deletes txtFile.txt

touch newTextFile.txt

touch creates the file with no content

top

Displays resources and process

Downloading

wget http://www.google.co.uk/index.html

wget downloads a file at a location the above line downloads googles index.html page

git clone https://github.com/joyent/node.git

git clone will clone a git repository to your current location

svn checkout http://as3-youtube-data-api.googlecode.com/svn/trunk/ as3-youtube-data-api-read-only

svn checkout checks out a svn repo

Editors
You will edit a lot of text files on server a far bit. So I will list 3 Degrees of editors.

Easy

pico textFile.txt

this one is easy all shortcuts (^ is ctrl) are listed at the bottom but not as powerful as the other two.

Medium

vi  textFile.txt

this is obviously more powerful than pico but to achieve that it uses states when u start you can’t type you need to enable typing. This is just a taster of how to use it. There are entire pages on the web that list all the shortcuts.

i
press i when u are not in edit mode and it will insert the cursor to allow you to type

press ESC then :wq
esc enters command mode w stands for write and q stands for quite so save and exit

press ESC then :q!
quit with out saving

Hard

vim  textFile.txt

best text editor i could write a whole post on it. If you wana use google it.

Compiling Code
In Unix you can tailer a build for your system. so its compiled for you and you alone :) Lets say you downloaded a project from the internet using git or svn.

Complete in order below

cd
to the location of the files

./configure

set settings in the build for your machine its automated it just does it


make

compiles your code

make check

tests the compile

make install

installs your compiled code

make clean

Removes all the compiled files and cleans up for you

Comming soon :)  sudo pico /etc/motd

Filed under Mac + OSX, Node.js, Server, ssh, unix

Node.js

0 Comments

links: Node.js npm forever

Over the years I have dipped in and out of a few different server side languages. PHP, Ruby, ColdFusion and Java to mention some. I guess the root of my issues with them is how infrequently I need to use them. It does not help that none of them share any development paradigms outside OO.

Some one mentioned Node.js to me when I discussed some ideas I was toying with. I immediately blanked them on the grounds that JS is client side and I specifically had a server side problem to resolve. I brushed the dust of my PHP book and got to work.

As what always happens to me when I am building things in PHP I ran into problems. As always I vent at a few friends to blow of some steam. As I did this people I knew kept on mentioning Node.js.

I eventually caved and looked it up. What no one had mention or in some case I didn’t give them a chance to mention. Is that Node.js is Googles V8 engine running on a server doing all those pesky backend Networking tasks in JavaScript. A Open Source ECMAScript platform will the internet ever ceases to amaze me.

After watching the video on the node.js website see link above I was flabbergasted at how easy it was. I figured I could build my solusion in no more than one day. I installed it on my server. Which took  all of 10 min. no really thats how long it took me to download the files compile the latest build and install it. It was disgracefully fast.

So I got to work. One and a half hours later I was was looking at my editor thinking surely i cant be done there must be now way its that easy to set up two TCP servers and an http server. Yet it was done plain as day all running and working.

If you are going to give it a try i recommend npm which is the plugin manger for Node.js and using it to install forever which helps you manege which servers you want to keep up. all links are at the top of the post. Obviously i would recommend it. I have even heard it runs on some shared hosts admittedly a little cut down but more than enough to inspire.

cocos2d: WARNING: format is not supported for CCSpriteFrameCache

1 Comment

I have been working away at same iPhone game development.

I found a nice sprite sheet editor zwoptex: http://zwoptexapp.com which is compatible with the engine I was using cocos2d: http://code.google.com/p/cocos2d-iphone/

Little did I know there was some version issues between the exporter and the engine. Zwoptex exports a png and a plist that contains all the meta-data for the sprite sheet. If you are getting the bellow mentioned error you might be reading online various suggestions like installing older versions of zwoptex or a new versions of the  engine.

Solution:

As it turns out all you have to do is un-check  rotate from inside zwoptex publish the files. Edit the plist generated by zowptex and change the root>>metadata>>format number from 2 to 1 save clean your build recompile and your done.

cocos2d error:

ocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:

Full Error:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:’
2010-09-13 16:01:36.353 TileMap[12915:40b] Stack: (
4768532,
450434,
452386,
10372,
298606,
9808,
8936,
8317326,
8321359,
8346942,
8328439,
8360408,
8319521,
8352626
)

Password Protecting Mail.app and your e-mails

0 Comments

One of the issues I had with using the Mail.app on OSX is its lack of security.

If some one is logged in as you they then get access to everything else on you machine like say your mail client.

Now I really like the Mail client. I have no intention of switching clients. So a solution was required.

After a little digging i found there was no mods for Mail. No hacks or any other means in which to password protect the client and the mail. Eventual I realised I was doing it all wrong its not the emails and the client that can be protected but where they are being stored is what can be protected.

I finally realised if I created a encrypted dmg and ran everything from there it would yield the same results.

Aim:
Get both the Mail.app and my email onto a password protected drive.

Process:

Open Disk Utility to create the encrypted DMG /Applications/Utilities/Disk Utility.app Click “New Image” on the top Select a Name and a save as location Further down under size select the size I made mine 10GB  this depends on how much mail you expect to store locally Below that make sure select the Encryption Hit create set the password and now you should have a mounted encrypted drive Move your Mail app  from /Applications/ to the root of your mounted encrypted drive cut it don’t copy it Then make make an alias of the Mail app in the in the /Applications/ folder(ctrl dragging the icon) Now you need to cut and past your emails to the encrypted drive find the folder /Users/YOUR_USERNAME/Library/Mail and past it in the encrypted drive Now make an alias of the mail folder from you encrypted drive  back to the /Users/abraham.azam/Library/ (ctrl dragging the folder) Finished.

If you click the mail icon on the Dock it should now access your mail client and your amils from a scure and ecrypted location. if you unmount the drive and click the icon it will promt you for the password to the dmg then opens the mail app.

please let em know what you think of these steps :)

Filed under Mac + OSX, Security

Windows 7 Mobile SDK

1 Comment

Mix is upon us ons again. Microsoft has finally released the Windows 7 Mobile SDK which can be found here. There is a supporting site with information here about the new platform and some examples.

Win7 Mobile is not built on previous windows mobile platform but a clean concept. The development tools are new to mobile too. It utilises  Silverlight and XNA for application and games development. So it will start with a pretty big developer base.

I was hoping to get a peak at the new Windows Mobile OS from the emulator but all I found was the  browser. The UI and other things are there. The ability to compile and tests apps so early is also very nice put none of the rumoured integration with core apps features seem to be available yet.  You do on the other hand get to see what the feel of there user journey is like and how the menus and navs will work.

All and all worth a look.

Edit:

Here is some new Blend 4 + Windows 7 Mobile plugin in for Blend

and

Here is some nice tutorials.


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser