BuySellAds.com

If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C
Jump

Category Archive for ‘Q&A’ rss

"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.

How to Rename an Appthe

Andreas asks:

I need to rename my app to something else because the original name is a trademark of a UK-based company. After I replaced all instances of the name where I found it the name of the product is still the same. Now if I build the products stay red and I get “No launchable executable present at path.” I have changed the name EVERYWHERE. How can I fix that?

Apart from the quick fix that I found googling I am going to see if I can fix this in XCode myself. I’ll show all the steps for guidance and the “pro fix” at the end.

Read more

Display Local HTML file in UIWebView

Stefan87 asks:

Is it possible to display a HTML File which was created by the application in a UIWebview?

I would like to provide a helpfile in HTML-Format in the webview.

I’ve read the documentation, but it seems that every loadprocess requires a URL ?

I wanted to do the very same thing for displaying the instructions in LuckyWheel. After some experimentation I found the following way to be the simplest for me.

Read more

Wait a Week

Moshe asks:

“How can make my app show a message 1 week after an event? I want to show an alert view the first time the app is launched after at least 1 week has expired and the event is still true.”

This technique can be used for any kind of action you don’t want to do right away or somehow should be based on a value determined in a previous run of your app.
Read more

Sending E-Mail Attachments

Ravikumar Gajjavelly asks:

I want to send email It contains image as an attachment.How can we send am email.how to attach image.can anybody please give solution.

When creating my app GeoCorder I was faced with the exact same dilemma of needing to attach a file to an email. I did some research and this is what I found.

Read more

Should I Release Outlets?

bqbqhaha asks:

Let me ask you something as below.

  1. A variable declared as IBOutlet could be released?
  2. A variable declared as IBOutlet should be released?

I search for this issue many times, but still the answer is not found.
plz help me , plz

He is asking a question that most of us have when beginning with Interface Builder and Outlets. In plain English his question is “Should I release instance variables declared as IBOutlet?”

Read more

Date Closest to 'NOW'

Nagarajan asks:

I have needed a SQLite query that returns exactly a time which is very closest to the current time.

From 7:00pm,8:00pm, 9:00am and 10:00pm if the current time is 8:30pm, I will require 10:00pm.

This is more of a SQLite question than related to Cocoa Touch. But since SQLite is the mobile database engine of choice for Cocoa Touch, I’ll entertain this question as well. And follow it up with some Cocoa Code.

Read more

Adding Numbers to an Array

reembertoparada asks:

I have a loop from 1 to 30 and I want to create an NSArray with those values inside. Can you give me an idea on how to do this.

That’s an easy after-dinner exercise. First you need to take an NSMutableArray instead of an NSArray because regular objects without “mutable” in their class name cannot be changed. This is especially vital to know for arrays and dictionaries.

Read more

Adding Days to an NSDate

Nagarajan asks:

I just want to get a date which is some 50 or 60 days away from current date.

How do i get??

There are two methods to achieving this. One that is quick and (potentially) dirty. And one that is always safe.

Read more

How to Fix Code Signing Errors

Andreas asks:

With Device | Debug and Code Sign “iPhone Developer” I have no problems getting my app onto the device, but with all other configurations I get this strange error message in XCode Organizer.
Unexpected Error

That’s a weird error that most of us have encountered one time or the other. Here are my hints how to get it fixed.

Read more