Monday, December 13, 2010

For/Foreach -> php vs objective-c

Lets start with a short Post about for/foreach PHP vs Objective-C
 To loop through an array in php is very simple:

foreach($arrData as $key => $value){
    //do something...
}

or

for($i = 0; $i < count($arrData); $i++){
    //do somethin... 
}

In Objective-C you have similar possibilities but you have to be sure what kind of object you are dealing with e.g. NSDictionary* or NSArray*

To get an Element of these two types you do as follows:

NSDictionary

NSDictionary* arrData = ...;
 [arrData objectForKey:@"id"];

Loop

for (NSString* key in arrData) {
        id value = [arrData objectForKey:key];
}

NSArray


NSArray* arrData = ...;
 [arrData objectAtIndex:1];

Loop

for (int i = 0; i < [arrData count]; i++) {
     [arrData objectAtIndex:i];
}

Besides these options you can also use enumerateKeysAndObjectsWithOptions. But I personaly dislike it cause it "feels" bad.

If you have an array of similar objects and you want all of them to perform the same selector you can also do this:

[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

This will save you some lines of code and is very "readable".

Cheers

JayEs

2 comments:

  1. RIZE is known for its web development services world-wide and PHP Development
    is powerful area of ours. Basically designed for the creation of dynamic web pages and thereby website development.

    ReplyDelete