В Objective-C все методы начинаются с символа «-» или «+». Пример:
@interface MyClass : NSObject
// instance method
- (void) instanceMethod;
+ (void) classMethod;
@end
Символы «+» и «-» указывают, является ли метод class method
или instance method
соответственно.
Разница была бы ясна, если бы мы назвали эти методы. Здесь методы объявлены в MyClass
.
instance method
требуется экземпляр класса:
MyClass* myClass = [[MyClass alloc] init];
[myClass instanceMethod];
Внутри MyClass
другие методы могут вызывать методы экземпляра MyClass
, используя self
:
-(void) someMethod
{
[self instanceMethod];
}
Но, class methods
должен быть вызван для самого класса:
[MyClass classMethod];
Или:
MyClass* myClass = [[MyClass alloc] init];
[myClass class] classMethod];
Это не будет работать:
// Error
[myClass classMethod];
// Error
[self classMethod];
There is usually a %10s
kind of printf
scheme that formats nicely.
However, I have not used ruby at all, so you need to check that.
Yes, there is printf with formatting.
The above example should right align in a space of 10 chars.
You can format based on your widest field in the column.
printf ([port, ]format, arg...)
Prints arguments formatted according to the format like sprintf. If the first argument is the instance of the IO or its subclass, print redirected to that object. the default is the value of $stdout.
Если вы знаете, что максимальная длина не должна превышать 20 символов:
printf "%-20s %s\n", value_name, value
Если вы хотите сделать его более динамичным, то должно работать что-то вроде этого:
longest_key = data_hash.keys.max_by(&:length)
data_hash.each do |key, value|
printf "%-#{longest_key.length}s %s\n", key, value
end
You typically don't want to use tabs, you want to use spaces and essentially setup your "columns" your self or else you run into these types of problems.