BuySellAds.com

My book Barcodes with iOS 7 is nearing completion. Buy it now to get early access!
Our DNA is written in Objective-C
Jump

Understanding Autoreleasing

Do you really understand when you need to retain and release some object? And what about autorelease? It took me around 3 months before I grasped when to use autorelease and now it’s second nature to me. But when I needed to explain to a coding buddy I was stumped. I found that my approach was one of feeling rather than knowledge.

So I decided it was time to do an experiment to visualize what happens to the retain count and when an instance is really released. So see what’s happening to our crash test dummy class I made a TestClass that overrides all the important methods so that we get an output to NSLog what is happening.

First I want to demonstrate how the retain counter – present in all Objective-C classes – is used to automatically determine when the instance can be freed from memory. Then I am going to show how to use a factory method which returns an autoreleased instance to make your life easier.

Read more

MyAppSales 1.0.5

First day back from vacation I chose an easy thing to do to get back into the saddle. Nothing too complicated because I don’t want to relapse into needing another vacation right away. I fixed a bug in MyAppSales and bragged blogged about it. This formal release contains these updates:

  • CHANGE: All __MyCompanyName__ now changed to drobnik.com
  • FIXED: Login problems after Apple updated iTunes Connect (quick-fixed previously)
  • FIXED: Password not getting properly URLencoded

The last part is especially necessary for you if you have a plus sign or other characters in your password which are not safe in URLs. If you previously experienced such problems please accept my apologies if you managed to lock your iTunes account. This update should fix this problem for you.

The changes which had been necessary to deal with Apple’s update for iTunes Connect had previously been in the repository but now they have a formal version number. As usual you can update your copy from the SVN customer-only repository.

URL Encoding

When transmitting data in the context of the HTTP protocol you often need to encode text in a way that does not interfere with special characters used in URLs. This is of importance if you want to put unicode characters into an URL query but also for simple things like constructing a body for a HTTP POST request. A form post also takes the form fields and puts them into the form that you know from an URL: field=text&another=more. That’s what the HTML content type “application/x-www-form-urlencoded” means.

The first thing that jumps out of the documentation when looking for a standard function to achieve such “URL Encoding” is stringByAddingPercentEscapesUsingEncoding. So that is what I was using for encoding the password for my iTunes Connect class which drives MyAppSales. And until now this worked without a hitch until customer #113 who was the first to use a plus character in his password. The poor guy ended up locking his iTunes account. Sorry!

It turns out that + is an anachronistic special character substitution for a space. I would have expected for it to be encoded properly by the above mentioned method as %20, but this is not the case.

Read more

MyAppSales – Quick Fix

On the very first day of my vacation Apple updated the iTunes Connect site breaking all existing report downloaders.

My hero is Lukas Matablewski who figured out the necessary changes. Stefan Sorin Nicolin, who is hosting the MyAppSales svn repo commited the changes to the trunk. So please update your working copy now.

All of this while I am in a beautiful NO SIGNAL region of Austria…

Out of Office

I’m out of office while I am on summer vacation with my wife catching up on some honeymooning. I will be back mid August.

Huegel

Until then I will only be able to sporadically check my e-mails and/or respond.

If you have donated for MyAppSales: after the PayPal payment you see a link that takes you back to drobnik.com where you can read the instructions on how to get it. If you missed that then e-mail me, but it will take a while until I respond.

If you have donated for AntiCrack is: Please be patient, I’ll mail it to you as quickly as I get a chance.

All other questions: Please ask them again mid of August when I am back in office.

Crashed MacBook Gets New Lease of Life

I previously reported on the unfortunate encounter of my first MacBook Pro with stone floor. Those alu guys are sturdy little bastards I can now say from experience because it continued to work flawlessly.

Then came my birthday and a new unibody MacBook Pro to replace it. This is two unibody generations newer. This one has an SD-Card slot, dual GPU, 2.8 GHz and 500 GB Harddisk. No SSD yet because for me 128 GB turned out to be too small and larger quantities are not yet financially viable.

Almost as long as I had the old MBP I also had provided a MacMini to my partner Christian whose been learning to code Cocoa on it. Turns out besides of being a Photoshop guru Christian also is well equipped with all screw drivers there are and with the patience and precision necessary to refurbish devices which are usually considered to be “beyond repair”.

Read more

Can Retro Survive the App Store?

Many of the typical iPhone users of today can remember the infancy of video games. First we went to arcades to play simple games made up of sprites or vectors. Many coins went into the slots that allowed you to play. Later companies like Nintendo started to make “Game&Watch” games which you could take with you. Their technical basis would always be an LCD screen where otherwise translucent areas would be made black opaque by electronics to manifest game characters. Movement would consist of several such on/off graphics switched in succession.

The iPhone and iPod Touch devices from today have orders of magnitude more computational power and for a modern game to be successful it has to feature fancy graphics, action and 3D. Or does it? I had a look at two games that like to be correlated to the “Retro” category while at the same time claiming to add “a new twist”. I was intrigued by such a bold statement and thus I am reviewing both apps in this article.

Have a look at those two candidate games and let me know if you think that Retro can be a viable category of games on the store. Or is it the past and should we be glad that it’s over?

Read more

"Are You a Cocoa Crack?" Quiz (1)

These questions will show if you are really the Cocoa Crack you like to believe to be. To see the answer highlight the answer text with your mouse. No peeking! Respond in the comments how many you got correct.

Quiz: Warm Up. Why can you not add an NSInteger to an NSArray? With which extra step can you do it?

Answer: NSInteger is only a different name for a signed integer. It is not a class even though the name might suggest it is. Only instances of classes can be added to NSArrays. The extra step mentioned is to first create an NSNumber object from the integer which you can add to an NSArray.


Quiz: I have an outlet defined and my code compiles without error or warning. But when I try to connect in in Interface Builder it does not show up. Why?

 
@interface CocoaQuizViewController : UIViewController
{
	UILabel IBOutlet *myLabel;
}
 
@property (nonatomic, retain) UILabel IBOutlet *myLabel;
 
@end

Answer: The IBOutlet is at the wrong position. It needs to be before the type UILabel. IBOutlet itself is defined to be replaced with empty text for compiling, that’s why it does not cause a syntax error. But Interface Builder is looking for this keyword in header files to know what outlets there are.


Quiz: What does the following code do? Variable age is defined as NSInteger. Is the syntax correct or is there something missing? It compiles fine, but why does it crash?

myLabel.text = age?[NSString stringWithFormat:@"%@", age]:@"";

Answer: If the variable age is not equal to zero then the result of stringWithFormat will be assigned to the label text. If it is zero then an empty string will be assigned. The format argument is wrong, %@ gets the description of an instance. Since age is probably a number the format argument needs to be %d. It crashes because objC tries to access the memory at position “age” which the app does not own and therefore causes an exception.


Quiz: The following three methods all allow you to connect the outlet in Interface Builder. What is the technical difference between them and which is the method recommended by Apple?

Variant 1:

@interface CocoaQuizViewController : UIViewController
{
	IBOutlet UILabel *myLabel;
}
@end

Variant 2:

@interface CocoaQuizViewController : UIViewController
{
	IBOutlet UILabel *myLabel;
}
 
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end

Variant 3:

@interface CocoaQuizViewController : UIViewController
{
	UILabel *myLabel;
}
 
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end

Answer: Method 3 is the correct one that should be preferred according to Apple. The instance that gets loaded from the XIB gets retained, just in case which prevents accidental unloading in case of low memory. Method 2 has a superfluous IBOutlet next to the instance variable because in this case the compiler will use the property anyway. Method 1 also can be connected, but it does not retain the loaded instance. This might cause problems if the instance gets deallocated for some reason.


Quiz: Complete this code so that it becomes syntactically correct using no more than one additional word and two square brackets.

NSInteger myNumber = @"1000";

Answer: @”1000″ is in itself a NSString instance. Therefore you can use it as receiver for the method integerValue. NSInteger myNumber = [@"1000" integerValue];


Quiz: (Bonus Question) Why does the following code crash as soon as the property is set? What is the simple fix?

MeasureStripView.h

@interface MeasureLinealView : UIView
{
	NSUInteger minValue;
}
 
@property (nonatomic, assign) NSUInteger minValue;

MeasureStripView.m

#import "MeasureStripView.h"
 
@implementation MeasureStripView
 
@synthesize minValue;
 
 
#pragma mark Passthrough Properties
- (void) setMinValue:(NSUInteger)aVal
{
	// this overriding is absolutely necessary
	self.minValue = aVal;
}

Some other file

MeasureStripView *myMS;  // assume it is properly initialized and otherwise working
myMS.minValue = 100;   // this line crashes. If commented out the app works fine

Answer: Using self.minValue as an lvalue cases the setter method setMinValue to be called. This causes an endless loop running out of memory eventually. The fix is to remove the “self.” inside the setter because minValue is an instance variable and thus accessible just by it’s name throughout the class.

So how many did you catch? Be honest! If you also have a Cocoa riddle like these to contribute please mail them directly to me (oliver@drobnik.com) and I will publish them in this format.

Boosting Your Sales Rank

Michael Dorn of Applyzer.com has posted info on an interesting experiment he performed. The goal was to see how a massive outlet of promo codes would be able to affect the sales rank. While such a practise surely might get frowned about by big Apple, it is commonplace amongst smaller development groups and individuals to trying to increase overall sales.

Chart

The times mentioned on this chart (Games/Trivia US) are central European time zone and document the last 13 hours. You can clearly see the jump in sales rank 11 hours ago with a steady movement toward the “visible sweetspot”.

Read more

Visit WWDC '09 … Retroactively!

Most of the world sees the annual WWDC as one of several fixed occasions where new and exciting stuff is announced or released. But the meat of this Worldwide Developer’s Conference (as the name suggests) is about training.

Being able to attend the WWDC is the dream of many new iPhone developers, as the numbers have shown. 60% of the total 5200 attendees to this years WWDC have never been to the conference before. But attending is expensive. Besides of travel and hotel expenses the ticket alone costs in the vicinity of $2000. Who can really afford to spend a month’s salary on this conference sacrificing a week of vacation time in tandem?

When I got Apple’s notification two days ago that the session videos are now available for sale my heart jumped and my brain got fuzzy thinking whether I should take the plunge and spend $299 for just the iPhone stream or $499 for both iPhone and Mac.  So finally after checking my savings and finding several excuses for spending the money like “It’s training and that’s priceless” I spend 429 Euros on the complete package.

Read more