Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a simpler Objective-C solution

Tags:

objective-c

Maybe simpler isn't the proper word; terse might do as well. I'm well-aware that Objective-C is a verbose language; I'm just wondering if it has to be as verbose as it is in the solution I'm showing below. I'm relatively new to the language.

This piece of code counts the number of each type of field in a record, pursuant to generating a variable-height UITableViewCell containing one label per field. My question isn't about how to do this -- I've already figured that out -- I'm just wondering: isn't there a simpler or less verbose way of doing this in Objective-C, using an NSDictionary object? Is this as terse as it gets using an NSDictionary? Solutions using other collection-type objects are also welcome.

Here's my -PartsRecord.countFields method. I've simplified the code slightly.

-(NSDictionary*) countFields {  
NSDictionary* dict = [[NSDictionary alloc] initWithObjectAndKeys:  
     @"s", [NSNumber numberWithUnsignedInt: [self.struts count]],  
     @"h", [NSNumber numberWithUnsignedInt: [self.headAssemblies count]],  
     @"l", (self.leftVent == nil) ?   [NSNumber numberWithUnsignedInt: 0] :  
                                      [NSNumber numberWithUnsignedInt: 1],  
     @"r", (self.rightVent == nil) ?  [NSNumber numberWithUnsignedInt: 0] :  
                                      [NSNumber numberWithUnsignedInt: 1],  
     nil ];  
return dict;  

}

Any errors in the above code are errors in transcription , not actual errors in the code.
TIA,
Howard

like image 316
hkatz Avatar asked May 06 '26 11:05

hkatz


1 Answers

@"l", [NSNumber numberWithUnsignedInt: self.leftVent  ? 1 : 0],
@"r", [NSNumber numberWithUnsignedInt: self.rightVent ? 1 : 0],

will shorten it by 2 lines

like image 77
cobbal Avatar answered May 08 '26 09:05

cobbal