我有一個類,它有一些只能在類中使用的方法。存在這些方法,因爲我有三個步驟對圖形工作我做,但我只想類的實例來訪問這些計算的最終結果,在一個簡單的例子:如何編寫只能在類中使用並能夠訪問ivars的方法
#import <Foundation/Foundation.h>
@interface GraphicsWorld : NSObject
@property(nonatomic, strong) NSMutableArray *objects;
@property(nonatomic, strong) NSMutableArray *adjustedObjects
/* three methods I'll never use outside of this class
I want to find a way to get replace these methods.
*/
-(void) calcTranslation;
-(void) calcRotation;
-(void) calcPerspective;
/* the one method I'll use outside of this class */
-(NSMutableArray *) getAdjustedObjects;
@end
我可以定義C函數外面我實現這個的,但他們不會訪問屬性:
#import <Foundation/Foundation.h>
#import "GraphicsWorld.h"
void calcTranslation()
{
// I'm useless because I can't access _objects.
}
void calcRotation()
{
// Hey, me too.
}
void calcPerspective()
{
// Wow, we have a lot in common.
}
@implementation GraphicsWorld
-(NSMutableArray *) getAdjustedObjects
{
calcTranslation();
calcRotation();
calcPerspective();
return adjustedObjects;
}
@end
@bbum這肯定是。 – michaelsnowden