Google Analytics

Readability

BuySellAds.com

Our DNA is written in Objective-C
Jump

Category Archive for ‘Q&A’ rss

Key-Value-Coding

Simon asks:

I would like to loop through variables whose names end with numbers 0 to 5. What is the correct syntax for the i? I have tried variable[i], variable + i, without success

I know two methods to achieve this:

1. Put your objects into an array and then enumerate or for-loop over those

// e.g. in awakeFromNib
playerNameLabels = [[NSArray arrayWithObjects:player1NameLabel, player2NameLabel, player3NameLabel, nil] retain];
 
for (i=0;i<3;i++)
{
     UILabel *nameLabel = [playerNameLabels objectAtIndex:i];
     // do something the the nameLabel
}

2. Use the Key-Value-Coding approach.

// in this object's header the strings are defined, here we intialize them
string1 = @"string1";
string2 = @"string2";
string3 = @"string3";
 
int i;
 
for (i=1;i<3;i++)
{
     NSString *aName = [NSString stringWithFormat:@"string%d", i];
     NSString *oneString = [self valueForKey:aName];
     NSLog(@"string %d is '%@' and has length %d", i, oneString, [oneString length]);
}

The dangerous thing about the latter might be that you have construct precisely the right name for the value. Or else you will get an exception:

*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<selectortestAppDelegate 0x524e80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key string0.’

But actually the second approach is how Cocoa does most KVC internally.

Localization Workaround

Trebor asks:

“When I add a new localization, there are just a few choices:

English

Japanese

French

German

I thought that the system needed “fr” to know it is French and “de” to know it is German?

Will it recognize the word “German” and know that is the same as a de.lprog?

If the answer is yes, does it know the full names of all languages?

For example, if I want to add Chinese, can I call the localization “Chinese” or do I need to call it zh?”

I believe the following to be true. Xcode is still putting in English as first localization if you make a file localizable. This works, but the “modern way” to do things (or so I read) is to use the two letter iso codes. The problem is, that if you remove the English then you can never add any new localization. That’s a bug in Xcode.

So I do it like this:

  • create the file
  • make it localizable
  • add localizations for all locales you require, including English: en, de, it, es, fr, nl
  • LEAVE English empty, but put English into the “en” locale

Do not remove the “English”, because then XCode will not allow you to add new localizations in the future.

String to Array of Characters

Michael asks:

“How do I split the characters in an NSString into an NSArray? In BASIC I could split a string with empty character, but in Object C this does not work”

One way to do it is to just get one character substrings:

NSString *s = @"Hello World";
int i;
NSMutableArray *m = [[NSMutableArray alloc] init];
 
for (i=0;i&lt; [s length]; i++)
{
	[m addObject:[s substringWithRange:NSMakeRange(i, 1)]];
}
 
NSLog([m description]);  // most NS* objects have a useful description
 
[m release];  // don&#039;t forget

In Objective C every time you need a starting point and then a length from there it’s call an NSRange. Conveniently there is also a *Make macro for most structures like this. So in this case you just NSMakeRange(position,length) and pass this to a substringWithRange.