Objective c 我将在哪里实现一个通用方法,供3个不同的视图控制器使用?
我有3个视图控制器 每个控制器都需要对给定的核心数据对象执行计算 这些计算因对象类型和控制器而异 方法是,Objective c 我将在哪里实现一个通用方法,供3个不同的视图控制器使用?,objective-c,ios,generics,implementation,Objective C,Ios,Generics,Implementation,我有3个视图控制器 每个控制器都需要对给定的核心数据对象执行计算 这些计算因对象类型和控制器而异 方法是, performTimeOperations:(Year *) // VC1 performTimeOperations:(Month *) // VC2 performTimeOperations:(Day *) // VC3 每一行大约有50行代码 但是它们每一个的代码变化都很小,我真的很想传递一个ID,就像这样 performTimeOperations:(id) 让它处理我扔给它
performTimeOperations:(Year *) // VC1
performTimeOperations:(Month *) // VC2
performTimeOperations:(Day *) // VC3
每一行大约有50行代码
但是它们每一个的代码变化都很小,我真的很想传递一个ID,就像这样
performTimeOperations:(id)
让它处理我扔给它的每一种物体
主要是因为我做的每一个改变,我必须在三个地方做
您将在何处实施此功能?怎么做
我应该看看分类吗?我也不认为这应该出现在我的appDelegate中。。。但它肯定比有3个实现更好
任何建议都将不胜感激
谢谢大家!
Nuno为什么不让所有核心数据对象从实现此方法的公共基类继承 i、 e.代替
Day -> NSManagedObject
Month -> NSManagedObject
Year -> NSManagedObject
你会的
Day -> MyDateType -> NSManagedObject
Month -> MyDateType -> NSManagedObject
Year -> MyDateType -> NSManagedObject
为什么不让所有核心数据对象从实现此方法的公共基类继承 i、 e.代替
Day -> NSManagedObject
Month -> NSManagedObject
Year -> NSManagedObject
你会的
Day -> MyDateType -> NSManagedObject
Month -> MyDateType -> NSManagedObject
Year -> MyDateType -> NSManagedObject
您可以在对象中实现计算的不同部分,如@deanWombourne所说。或者,您可以在单个计算方法中检查对象类型(类)。这取决于你把这个方法放在哪里,你比我们更了解你的代码。也许您可以创建例如Calculator类并将计算方法放在那里。您可以在对象中实现计算的不同部分,如@deanWombourne所说。或者,您可以在单个计算方法中检查对象类型(类)。这取决于你把这个方法放在哪里,你比我们更了解你的代码。也许您可以创建例如Calculator类并将计算方法放在那里。两个选项,C方式:
id doYourThing(id arg) {
//50 lines of code on the screen
//50 lines of code on the ...
return anAnswer;
}
静态方法包括:
@interface AnAppropriateClass : NSSomething
+ (id) doYourThing: (id) arg;
@end
@implementation AnAppropriateClass
+ (id) doYourThing: (id) arg {
//50 lines of code on the screen
//50 lines of code on the ...
return anAnswer;
}
@end
这两个都是
两个选项,C方式:
id doYourThing(id arg) {
//50 lines of code on the screen
//50 lines of code on the ...
return anAnswer;
}
静态方法包括:
@interface AnAppropriateClass : NSSomething
+ (id) doYourThing: (id) arg;
@end
@implementation AnAppropriateClass
+ (id) doYourThing: (id) arg {
//50 lines of code on the screen
//50 lines of code on the ...
return anAnswer;
}
@end
这两个都是
助手类是我的第二个选择,事后看来,单元测试您的解决方案比我的要容易得多:)+1!很好,@mifki我确实尝试过实现一个类“时间计算器”,但它看起来很孤独,没有归属感。看起来不太对lolA helper类是我的第二个选择,事后看来,单元测试你的解决方案比我的要容易得多:)+1!很好,@mifki我确实尝试过实现一个类“时间计算器”,但它看起来很孤独,没有归属感。看起来不对,哈哈