Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JoostK
Created July 24, 2010 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoostK/488754 to your computer and use it in GitHub Desktop.
Save JoostK/488754 to your computer and use it in GitHub Desktop.
Generates KVC compliant property accessors
<?php
define('_OBJC_CLASS', 'ClassName');
define('_OBJC_PROPERTY', 'propertyName');
class KVCMethodsGenerator {
private $_class;
private $_propertyName;
private $_ivar;
private $_ucfirstPropertyName;
public function __construct($class, $propertyName){
$this->_class = $class;
$this->_propertyName = $propertyName;
$this->_ivar = '_' . $propertyName;
$this->_ucfirstPropertyName = ucfirst($propertyName);
}
public function header(){
return
<<<HEADER
@interface {$this->_class} ({$this->_ucfirstPropertyName}Property)
@property(readonly) NSMutableArray *mutable{$this->_ucfirstPropertyName};
- (NSUInteger)countOf{$this->_ucfirstPropertyName};
- (id)objectIn{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index;
- (NSArray *){$this->_propertyName}AtIndexes:(NSIndexSet *)indexes;
- (void)insertObject:(id)object in{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index;
- (void)insert{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName} atIndexes:(NSIndexSet *)indexes;
- (void)removeObjectFrom{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index;
- (void)remove{$this->_ucfirstPropertyName}AtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectIn{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index withObject:(id)object;
- (void)replace{$this->_ucfirstPropertyName}AtIndexes:(NSIndexSet *)indexes with{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName};
- (void)add{$this->_ucfirstPropertyName}Object:(id)object;
- (void)add{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName};
@end
HEADER;
}
public function implementation(){
return
<<<IMPLEMENTATION
@implementation {$this->_class} ({$this->_ucfirstPropertyName}Property)
- (NSMutableArray *)mutable{$this->_ucfirstPropertyName} {
return [self mutableArrayValueForKey:@"{$this->_propertyName}"];
}
- (NSArray *){$this->_propertyName} {
return [NSArray arrayWithArray:{$this->_ivar}];
}
- (void)set{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName} {
if(!{$this->_ivar}){
{$this->_ivar} = [{$this->_propertyName} retain];
} else {
[{$this->_ivar} setArray:{$this->_propertyName}];
}
}
- (NSUInteger)countOf{$this->_ucfirstPropertyName} {
return [{$this->_ivar} count];
}
- (id)objectIn{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index {
return [{$this->_ivar} objectAtIndex:index];
}
- (NSArray *){$this->_propertyName}AtIndexes:(NSIndexSet *)indexes {
return [{$this->_ivar} objectsAtIndexes:indexes];
}
- (void)insertObject:(id)object in{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index {
[{$this->_ivar} insertObject:object atIndex:index];
}
- (void)insert{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName} atIndexes:(NSIndexSet *)indexes {
[{$this->_ivar} insertObjects:{$this->_propertyName} atIndexes:indexes];
}
- (void)removeObjectFrom{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index {
[{$this->_ivar} removeObjectAtIndex:index];
}
- (void)remove{$this->_ucfirstPropertyName}AtIndexes:(NSIndexSet *)indexes {
[{$this->_ivar} removeObjectsAtIndexes:indexes];
}
- (void)replaceObjectIn{$this->_ucfirstPropertyName}AtIndex:(NSUInteger)index withObject:(id)object {
[{$this->_ivar} replaceObjectAtIndex:index withObject:object];
}
- (void)replace{$this->_ucfirstPropertyName}AtIndexes:(NSIndexSet *)indexes with{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName} {
[{$this->_ivar} replaceObjectsAtIndexes:indexes withObjects:{$this->_propertyName}];
}
- (void)add{$this->_ucfirstPropertyName}Object:(id)object {
[self insertObject:object in{$this->_ucfirstPropertyName}AtIndex:[{$this->_ivar} count]];
}
- (void)add{$this->_ucfirstPropertyName}:(NSArray *){$this->_propertyName} {
[self insert{$this->_ucfirstPropertyName}:{$this->_propertyName} atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange([{$this->_ivar} count], [{$this->_propertyName} count])]];
}
@end
IMPLEMENTATION;
}
}
$generator = new KVCMethodsGenerator(_OBJC_CLASS, _OBJC_PROPERTY);
echo '// ' . _OBJC_CLASS . '.h' . PHP_EOL;
echo $generator->header() . str_repeat(PHP_EOL, 3);
echo '// ' . _OBJC_CLASS . '.m' . PHP_EOL;
echo $generator->implementation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment