English 中文(简体)
通过自食其力的方法
原标题:Calling class methods via self

我的班级方法有些相当长(TimingConfigController是准确的),我经常从班级(例:[TimingConfigController intervalStringAt:i]),但班子名称长,我是因为发言时间长,而且与所有线人相比,读得更少。

I was reading up to see if I could find a shorthand alternative, and I came upon this article on developer.apple.com: Defining a Class

除其他外,它指出:

id newInstance = [[self alloc] init];

成为同一类物体的极好办法。 就我所知,该声明采用一种使用自我点而不是阶级名称的分类方法,因此,我试图在我看来这样做,尽管它可能发挥作用,但它会给汇编者发出警告。 我确实不想有警告。

因此,我的问题是,是否自称采用一种可能和/或好的想法? 是否有更好的替代办法? 在文件一开始,我是否真心自封? 或者,我是否只去做我的med子,生活在无情之中?

最佳回答

<代码>自<>/代码>仅代表一个班级方法内的班级。 由于班级方法的目标为班级,自行即为班级。

例如,在<条码>+(避免)初始化方法中,通常可以看到:

+ (void)initialize {
    if (self == [MyClass class]) { // <- self is the current class
        // ...
    }
}

如果你真想在不重新命名的情况下缩短班级名称,最简单的解决办法是使用<代码>#define:

#define TCC TimingConfigController

// The two lines are equivalent
id result1 = [TimingConfigController theClassMethod]; 
id result1 = [TCC theClassMethod];
问题回答

< [[自封] init]只适用于班级方法,而不是例如方法。 如果你有某种检查方法,而且你想制造同一类物体,你应使用:

id newInstance = [[[self class] alloc] init];




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签